Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro HELP... DisplayDimension and Dimension objects

Status
Not open for further replies.

SWVB101

Mechanical
Jun 25, 2003
85
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...

Code:
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-
 
Replies continue below

Recommended for you

OK... Here is a work around I came up with...

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...

Code:
  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
 
I looked into this a little. I couldn't find any accessors to get the Display Dimension from the Dimension. I think the reason for this is that it is possible for a single Dimension object to be parent to multiple instances of Display Dimension objects.

[bat]All this machinery making modern music can still be open-hearted.[bat]
 
That is what I concluded also...

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
 
Be *very careful* working with dims.. Ive written code that does all sorts of things with dims, and SW will allow you to totally HOSE your model if not careful (Like you can change the name of the dim for ONE CONFIG and not another..
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.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor