×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Macro HELP... DisplayDimension and Dimension objects

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-

RE: Macro HELP... DisplayDimension and Dimension objects

(OP)
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...

  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

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.

All this machinery making modern music can still be open-hearted.

RE: Macro HELP... DisplayDimension and Dimension objects

(OP)
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

RE: Macro HELP... DisplayDimension and Dimension objects

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources