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
Procedure
- Get Session from IpfcAsyncConnection
- Get the current model and convert it into Model2D since it is a drawing
- Get the drawing view name where to create the dimension
- Get model from the view
- 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
- Get the edges programmatically by passing edge ids
- Store the edges in attachment collection
- 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
- Create the dimension create instructions and apply the attachments, senses, dimension location and orientation
- Create dimension
Sample Code
Private Sub btn_CreateDrawingDimension_Click(sender As Object, e As RoutedEventArgs) Try 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 Next 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( Attachments, Senses, DimLocation, SelectedOrientation) Dim Dim2D As IpfcDimension2D = Model2Ds.CreateDrawingDimension(IDrawingDimCreateInstruction) Catch ex As Exception MessageBox.Show(ex.ToString, "Failure") End Try End Sub