I am trying to dimension distance from models edge to reference geometry axis on a section view via API(axis name is hardcoded to Axis1). I havent been able to acces the correct axis object that belongs to the same model as the edge, selected from GUI.
I can find Axis1 programmatically, but cannot select it in the correct drawing assembly-instance context.
To illustrate, I prepared 2 examples:
Test 1:
- selecting an edge on A-A section view and axis manually from feature tree
- drawing dimension with macro
Macro:
Option Explicit Sub Main() Dim swDoc As SldWorks.ModelDoc2 Dim swSel As SldWorks.SelectionMgr Dim swDim As SldWorks.DisplayDimension Dim swObj As Object Dim swDC As SldWorks.DrawingComponent Dim pt As Variant Dim axisName As String Dim selType As String Dim selTypeID As Long Set swDoc = Application.SldWorks.ActiveDoc Set swSel = swDoc.SelectionManager ' Selection 2 = Edge, Selection 3 = Axis pt = swSel.GetSelectionPoint2(2, -1) ' Edge component Set swDC = swSel.GetSelectedObjectsComponent4(2, -1) Debug.Print "Edge component: " & swDC.Name ' Axis component Set swObj = swSel.GetSelectedObject6(3, -1) swSel.GetSelectByIdSpecification _ swObj, axisName, selType, selTypeID Set swDC = swSel.GetSelectedObjectsComponent4(3, -1) Debug.Print "Axis: " & axisName Debug.Print "Axis component: " & swDC.Name ' Create dimension Set swDim = swDoc.AddDimension2( _ pt(0) + 0.025, pt(1) + 0.025, 0#) Debug.Print "Dimension (mm): " & _ swDim.GetDimension2(0).SystemValue * 1000# End Sub
Result: Dimension 33,2 gets drawn correctly
Edge component: Assem1-SectionAssembly-2-1/Assem1-1/Part2-1
Axis: Axis1@Assem1-SectionAssembly-2-1@/Assem1-1@Assem1-SectionAssembly-2
Axis component: Assem1-SectionAssembly-2-1/Assem1-1
Dimension (mm): 33,2426563906556
Test 2:
- selecting only edge on section view A-A manually, axis is selected via API (swModel.FeatureByName("Axis1"))
- drawing dimension with macro
Macro:
Option Explicit Sub Main() Dim swDoc As SldWorks.ModelDoc2 Dim swSel As SldWorks.SelectionMgr Dim swDim As SldWorks.DisplayDimension Dim swObj As Object Dim swDC As SldWorks.DrawingComponent Dim swView As SldWorks.View Dim swModel As SldWorks.ModelDoc2 Dim swAxis As SldWorks.Feature Dim swEdge As SldWorks.Edge Dim pt As Variant Dim edgeName As String Dim axisName As String Dim selType As String Dim selTypeID As Long Dim ok As Boolean Set swDoc = Application.SldWorks.ActiveDoc Set swSel = swDoc.SelectionManager ' Selection 2 = Edge pt = swSel.GetSelectionPoint2(2, -1) ' Edge name and component Set swObj = swSel.GetSelectedObject6(2, -1) Set swEdge = swObj swSel.GetSelectByIdSpecification _ swObj, edgeName, selType, selTypeID Set swDC = swSel.GetSelectedObjectsComponent4(2, -1) Debug.Print "Edge component: " & swDC.Name ' Get Axis1 from referenced model Set swView = swSel.GetSelectedObjectsDrawingView2(2, -1) Set swModel = swView.ReferencedDocument Set swAxis = swModel.FeatureByName("Axis1") ' Select axis programmatically swDoc.ClearSelection2 True ok = swView.SelectEntity(swAxis, False) ' Axis name and component Set swObj = swSel.GetSelectedObject6(1, -1) swSel.GetSelectByIdSpecification _ swObj, axisName, selType, selTypeID Set swDC = swSel.GetSelectedObjectsComponent4(1, -1) Debug.Print "Axis: " & axisName Debug.Print "Axis component: " & swDC.Name ' Append original edge ok = swView.SelectEntity(swEdge, True) ' Create dimension Set swDim = swDoc.AddDimension2( _ pt(0) + 0.025, pt(1) + 0.025, 0#) Debug.Print "Dimension (mm): " & _ swDim.GetDimension2(0).SystemValue * 1000# End SubResult: Only the dimension 60 of selected edge gets drawn.
Edge component: Assem1-SectionAssembly-2-1/Assem1-1/Part2-1
Axis: Axis1@Assem1-SectionAssembly-2-1@
Axis component: Assem1-SectionAssembly-2-1
Dimension (mm): 60
My assumption is dimensioning is failing because of the wrong context of the axis. I have tried many things and havent been able to make it work. I am using Solidworks 2024.
Has anyone encountered the same problem?
Is there any useful documentation on Solidworks objects when it comes to different section views in the drawing?
