VBA question

Discussion in 'SolidWorks' started by djrobb, Jan 21, 2005.

  1. djrobb

    djrobb Guest

    I would like to create macro buttons to change dimension values
    (precision) in a drawing from decimal to fractional, and one place to
    two place or four place to three place etc. I would like to be able to
    start the macro then select the dimension(s) to change.

    Otherwise right now I have to right click on a dimension and go into
    properties to set fraction or decimal display.

    Anyone know how to do this in VBA??

    I tried the following code, "Part.SetUnits swINCHES, swFRACTION, 64, 3,
    True" but it changes all dimensions not just the selected dimension....
    Any help would be great!

    Thanks
     
    djrobb, Jan 21, 2005
    #1
  2. djrobb

    Tin Man Guest

    VBA macros usually require pre-selecting the items before you start the
    macro. Below is code I got from:
    http://www.frontiernet.net/~mlombard/
    This code is to make the selected dimension(s) 3-place decimals. He had
    separate macros for 0 to 4 place and also macros for adding as well as
    subtracting one decimal place. I have tweaked the code a little bit,
    but Matt Lombard is the original author. There are lots of macros at
    that website (which currently seems to be down).
    Ken

    '******************************************************************************
    ' 3Place.swb
    '******************************************************************************
    Dim swApp As Object
    Dim Part As Object
    Dim SelMgr As Object
    Dim selCount As Integer
    Dim selType As Integer
    Dim retval As Variant
    Dim dimension As Object

    'These definitions are consistent with type names defined in
    swconst.bas and swconst.h
    '*** swUserPreferenceIntegerValue ***
    Const swSelDIMENSIONS = 14
    Const swUnitsLinear = 47 ' Millimeters=0, Inches=3
    Sub main()
    Set swApp = CreateObject("SldWorks.Application")
    Set Part = swApp.ActiveDoc

    Dim MyVar001, MyVar002, MyVar003, MyVar004, MyVar005 As Integer
    MyVar005 = Part.GetUserPreferenceIntegerValue(swUnitsLinear)

    If MyVar005 = 3 Then
    'For Inch Units
    MyVar001 = 3 'Primary Precision (Inch)
    MyVar002 = 3 'Primary Tolerance (Inch)
    MyVar003 = 2 'Alternate Precision (Metric)
    MyVar004 = 2 'Alternate Tolerance (Metric)
    Else
    'For Metric Units
    MyVar001 = 3 'Primary Precision (Metric)
    MyVar002 = 3 'Primary Tolerance (Metric)
    MyVar003 = 4 'Alternate Precision (Inch)
    MyVar004 = 4 'Alternate Tolerance (Inch)
    End If

    Set SelMgr = Part.SelectionManager()
    selCount = SelMgr.GetSelectedObjectCount()
    If (selCount > 0) Then
    Dim i As Integer
    For i = 1 To selCount
    selType = SelMgr.GetSelectedObjectType2(i)
    If (selType = swSelDIMENSIONS) Then
    Set dimension = SelMgr.GetSelectedObject3(i)
    Dim primaryPrec As Long
    Dim alternatePrec As Long
    Dim primaryTol As Long
    Dim alternateTol As Long
    retval = dimension.GetUseDocPrecision()
    primaryPrec = MyVar001
    primaryTol = MyVar002
    alternatePrec = MyVar003
    alternateTol = MyVar004
    retval = dimension.SetPrecision(False, primaryPrec,
    alternatePrec, primaryTol, alternateTol) 'line wrapped in newsgroup
    post
    End If
    Next
    End If
    End Sub
     
    Tin Man, Jan 21, 2005
    #2
  3. djrobb

    That70sTick Guest

    First, I recommend that you get a book and learn a little VB. It helps
    to get a grounding in object-oriented programming.

    Anyhow, Like VB, VBA allows one to have forms in a program. If you
    have a form shown "modeless", the program will allow you to access SW
    for selection. Then you will need to deal with selections. Learn how
    to manipulate the SelectionManager object.

    Your "Main()" module could be as simple as this:

    Sub Main()

    Load Form1
    Form1.Show vbModeless

    'program your code in Form1
    'Have a command button on the form to press
    'to start your process once
    'selections are made

    End Sub
     
    That70sTick, Jan 21, 2005
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.