Create a dimension between two edges using Creo VB API

Create a dimension between two edges using Creo VB API

Creating dimensions in the drawing is one of the mandatory processes while doing drawing automation. Here the article to demonstrate how to create a dimension between two edges but its id using Creo VB API


  1. Get Session from IpfcAsyncConnection
  2. Get the current model and convert it into Model2D since it is a drawing
  3. Get the drawing view name where to create the dimension
  4. Get model from the view
  5. Now select the origin to start dimension. Generally consider the coordinate system. Since it is predefined in the model, we can use the name of the CSYS to dimension start
  6. Get the edges programmatically by passing edge ids
  7. Store the edges in attachment collection
  8. Before creating the dimension, we need to sense the dimension orientation. It may be vertical, horizontal or slanted. Use the conditional clause to sense the type of dimension
  9. Create the dimension create instructions and apply the attachments, senses, dimension location and orientation
  10. Create dimension

Sample Code

 Private Sub btn_CreateDrawingDimension_Click(sender As Object, e As RoutedEventArgs)
            BSession = AConnection.Session
            Model = BSession.CurrentModel
            Model2Ds = CType(Model, IpfcModel2D) ' Convert the model to 2d since it is drawing

            Dim Attachments As New CpfcSelections
            Dim SelectFirstItem As IpfcSelection

            Dim IView As IpfcView2D = Model2Ds.GetViewByName("main_view") ' Drawing View to draw dimension
            Dim IModel As IpfcModel = IView.GetModel

            Dim IModelOwner As IpfcModelItemOwner = CType(IModel, IpfcModelItemOwner)
            For Each CSYS As IpfcModelItem In IModelOwner.ListItems(EpfcModelItemType.EpfcITEM_COORD_SYS)
                If CSYS.GetName = "CS0" Then ' CSYS Name
                    Dim CMSelect As New CMpfcSelect
                    SelectFirstItem = CMSelect.CreateModelItemSelection(CSYS, Nothing)
                End If

            Dim SelectSecondItem As IpfcSelection

            Dim Edge1 As IpfcModelItem = IModelOwner.GetItemById(EpfcModelItemType.EpfcITEM_EDGE, Integer.Parse("160")) 'Edge ID 1
            Dim Edge3 As IpfcModelItem = IModelOwner.GetItemById(EpfcModelItemType.EpfcITEM_EDGE, Integer.Parse("161"))  'Edge ID 2

            Dim EdgeSelect As New CMpfcSelect
            SelectFirstItem = EdgeSelect.CreateModelItemSelection(Edge1, Nothing)
            SelectSecondItem = EdgeSelect.CreateModelItemSelection(Edge3, Nothing)

            Attachments.Set(0, SelectFirstItem)
            Attachments.Set(1, SelectSecondItem)

            Dim Senses As New CpfcDimensionSenses
            Dim CESense As New CCpfcEmptyDimensionSense
            Dim ESense1 As IpfcEmptyDimensionSense = CESense.Create()
            Senses.Set(0, ESense1)
            Senses.Set(1, ESense1)

            Dim DimLocation As New CpfcVector2D

            Dim SelectedOrientation As Integer
            Select Case cb_Orientation.SelectedIndex  ' Identify the orientation 
                Case 0
                    SelectedOrientation = EpfcOrientationHint.EpfcORIENTHINT_VERTICAL
                    DimLocation.Set(0, IView.Outline.Item(0).Item(0) - 20)
                    DimLocation.Set(1, (IView.Outline.Item(1).Item(1) + IView.Outline.Item(0).Item(1)) / 2)
                Case 1
                    SelectedOrientation = EpfcOrientationHint.EpfcORIENTHINT_HORIZONTAL
                    DimLocation.Set(1, IView.Outline.Item(0).Item(1) - 20)
                    DimLocation.Set(0, (IView.Outline.Item(1).Item(0) + IView.Outline.Item(0).Item(0)) / 2)
                Case 2
                    SelectedOrientation = EpfcOrientationHint.EpfcORIENTHINT_SLANTED
            End Select

            Dim CDrawingDimCreateInstructions As New CCpfcDrawingDimCreateInstructions
            Dim IDrawingDimCreateInstruction As IpfcDrawingDimCreateInstructions = CDrawingDimCreateInstructions.Create(
            Dim Dim2D As IpfcDimension2D = Model2Ds.CreateDrawingDimension(IDrawingDimCreateInstruction)
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Failure")
        End Try
    End Sub

Leave a Reply

Contact Us