Macro HELP... DisplayDimension and Dimension objects
Macro HELP... DisplayDimension and Dimension objects
(OP)
In A SolidWorks drawing...
When you select a Dimension the SelectionManager returns an object with the type value of 14 (Dimension)
I want to be able to select a dimension in solidworks and press a button to change the precision of the dimension and it's tolerance...
to do this you must use the DisplayDimension::SetPrecision method...
But this method is ONLY for the DisplayDimension object and not the Dimension Object...
The DisplayDimension displays the value of the Dimension object...
But like I said above when you select a DisplayDimension in a drawing the Selection Manager Returns the Dimension Object...
If you have the DisplayDimension you can get the Dimension Value using DisplayDimension::GetDimension method
Does anyone know how to do the oposite and get the DisplayDimension from the object that the selection manager Returns, or a better way to do the required task? here is a code sipplet to start with...
Private Sub Command1_Click()
Dim swApp as object, Part as Object
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
Dim SelMgr As SelectionMgr, SelObj As Object, DDim As DisplayDimension
Dim L As Long, P As Long, Ret As Long
Set SelMgr = Part.SelectionManager()
If (SelMgr.GetSelectedObjectCount >= 1) Then
Text1 = ""
P = 2 'Precision
For L = 1 To SelMgr.GetSelectedObjectCount
Text1 = Text1 & SelMgr.GetSelectedObjectType(L) & "; "
If (SelMgr.GetSelectedObjectType(L) = 14) Then
'The Next Line Causes an error...
Set DDim = SelMgr.GetSelectedObject2(L)
Ret = DDim.SetPrecision(False, P, P, P, P)
End If
Next
End If
End Sub
Thanks,
-Josh-
When you select a Dimension the SelectionManager returns an object with the type value of 14 (Dimension)
I want to be able to select a dimension in solidworks and press a button to change the precision of the dimension and it's tolerance...
to do this you must use the DisplayDimension::SetPrecision method...
But this method is ONLY for the DisplayDimension object and not the Dimension Object...
The DisplayDimension displays the value of the Dimension object...
But like I said above when you select a DisplayDimension in a drawing the Selection Manager Returns the Dimension Object...
If you have the DisplayDimension you can get the Dimension Value using DisplayDimension::GetDimension method
Does anyone know how to do the oposite and get the DisplayDimension from the object that the selection manager Returns, or a better way to do the required task? here is a code sipplet to start with...
Private Sub Command1_Click()
Dim swApp as object, Part as Object
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
Dim SelMgr As SelectionMgr, SelObj As Object, DDim As DisplayDimension
Dim L As Long, P As Long, Ret As Long
Set SelMgr = Part.SelectionManager()
If (SelMgr.GetSelectedObjectCount >= 1) Then
Text1 = ""
P = 2 'Precision
For L = 1 To SelMgr.GetSelectedObjectCount
Text1 = Text1 & SelMgr.GetSelectedObjectType(L) & "; "
If (SelMgr.GetSelectedObjectType(L) = 14) Then
'The Next Line Causes an error...
Set DDim = SelMgr.GetSelectedObject2(L)
Ret = DDim.SetPrecision(False, P, P, P, P)
End If
Next
End If
End Sub
Thanks,
-Josh-






RE: Macro HELP... DisplayDimension and Dimension objects
it searches through the DisplayDimensions in the active view and matches the name to the selected Dimension object...
when it finds a match, it sets the precision...
*DTPrec (Dimension Tolerance Precision) is a ComboBox that has values such as...
.XX
.XXX
.XXXX
so if the current value is ".XX" then Len(DTPrec) - 1 is 2
If anyone finds a better way to do this... please let me know...
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
Dim SelMgr As SelectionMgr, SelObj As Object
Dim DDim As Dimension, VDims() As DisplayDimension
Dim L As Long, P As Long, Ret As Long, N As Long, D As Long
Set View = Part.ActiveDrawingView
D = View.GetDimensionCount
If D > 0 Then
ReDim VDims(D) As DisplayDimension
Set VDims(1) = View.GetFirstDisplayDimension
If D > 1 Then
For N = 2 To D
Set VDims(N) = VDims(N - 1).GetNext
Next
End If
End If
Set SelMgr = Part.SelectionManager()
If (SelMgr.GetSelectedObjectCount > 0) Then
P = Len(DTPrec) - 1
For L = 1 To SelMgr.GetSelectedObjectCount
If (SelMgr.GetSelectedObjectType(L) = 14) Then
Set DDim = SelMgr.GetSelectedObject2(L)
For N = 1 To D
If VDims(N).GetDimension.Name = DDim.Name Then
Ret = VDims(N).SetPrecision(False, P, P, P, P)
Exit For
End If
Next
End If
Next
End If
Part.WindowRedraw
Thanks,
-Josh
RE: Macro HELP... DisplayDimension and Dimension objects
RE: Macro HELP... DisplayDimension and Dimension objects
That is why I took the backwards aproach...
If you have a dimension selected there is an active view (if you are in a drawing...)
You can select the active view like this...
Set View = Part.ActiveDrawingView
You can then loop through the DisplayDimensions using GetFirst... and GetNext
Set VDims(1) = View.GetFirstDisplayDimension
...
Set VDims(N) = VDims(N - 1).GetNext
Next check the dim object returned by the selection manager to your array...
Set DDim = SelMgr.GetSelectedObject2(L)
For N = 1 To D
If VDims(N).GetDimension.Name = DDim.Name Then
Ret = VDims(N).SetPrecision(False, P, P, P, P)
Exit For
End If
Next
The only problem that might exist is if you try to select dimensions from different views and execute the above method...
Otherwise, it works pretty good... (though it is not my preferred method...)
Thnx,
-Josh
RE: Macro HELP... DisplayDimension and Dimension objects
so in one configuration a dim name could be "D1@Sketch1" and in another it could be "MyDim@Sketch1" ... good luck trying to track THAT bug down, haa haa.