6 Replies Latest reply on Feb 9, 2016 11:33 PM by Steve Johnson

    Select Origin for Autodimension Selected Drawing View Example (VBA)

    Steve Johnson

      I'm trying to create a macro based on the SW Example Autodimension Selected Drawing View Example (VBA).  I can't figure out how to select the model origin.  Can anyone help me?

       

      2015 SOLIDWORKS API Help - Autodimension Selected Drawing View Example (VBA)

       

      '-----------------------------------------------------------------

      ' Preconditions: Verify that the specified drawing document to

      ' open exists.

      '

      ' Postconditions:

      ' 1. Opens the specified drawing document.

      ' 2. Activates Drawing View1.

      ' 3. Selects a vertex.

      ' 4. Autodimensions the drawing view based on the

      ' selected vertex.

      ' 5. Examine the drawing.

      '

      ' NOTE: Because the drawing is used elsewhere, do not save changes.

      '------------------------------------------------------------------

      Option Explicit

      Dim swApp As SldWorks.SldWorks
      Dim swModel As SldWorks.ModelDoc2
      Dim swDrawing As SldWorks.DrawingDoc
      Dim swModelDocExt As SldWorks.ModelDocExtension
      Dim status As Boolean
      Dim fileName As String
      Dim errors As Long
      Dim warnings As Long
      Dim selmark As Long
      Dim ret as Long
      Sub main()
          Set swApp = Application.SldWorks    
          ' Open drawing document of part
          fileName = "C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\advdrawings\foodprocessor.slddrw"
          Set swModel = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocDRAWING, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
          Set swDrawing = swModel
          status = swDrawing.ActivateView("Drawing View1")
          Set swModelDocExt = swModel.Extension
          ' Select drawing view
          status = swModelDocExt.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
          ' Horizontal and vertical datum, or a vertex datum, baselines for
          ' dimension creation
          ' These are optional; if not selected, autodimension uses default datums,
          ' the leftmost and bottommost edges
          selmark = swAutodimMark_e.swAutodimMarkHorizontalDatum
          selmark = swAutodimMark_e.swAutodimMarkVerticalDatum
          selmark = swAutodimMark_e.swAutodimMarkOriginDatum
          ' Select a vertex
          status = swModelDocExt.SelectByID2("", "VERTEX", 0.20215546544586, 0.2496899375, 4.79999999998881E-03, True, selmark, Nothing, 0)   
          ' Autodimensions the drawing view based on the selected vertex
          ret = swDrawing.AutoDimension(swAutodimEntities_e.swAutodimEntitiesBasedOnPreselect, swAutodimScheme_e.swAutodimSchemeBaseline, swAutodimHorizontalPlacement_e.swAutodimHorizontalPlacementAbove, swAutodimScheme_e.swAutodimSchemeBaseline, swAutodimVerticalPlacement_e.swAutodimVerticalPlacementRight)
      End Sub
        • Re: Select Origin for Autodimension Selected Drawing View Example (VBA)
          Viktor Bovzdarenko

          Hi Steve

          the line in your code: status = swModelDocExt.SelectByID2("", "VERTEX", 0.20215546544586, 0.2496899375, 4.79999999998881E-03, True, selmark, Nothing, 0)

          is supposed to select the vertex for you.

          If it cannot do so then you can record a macro of selecting a vertex manually. In recorded by you macro find corresponding line for selection of the vertex (it will have the same structure but different coordinates: status = swModelDocExt.SelectByID2("", "VERTEX", XXX, YYY, ZZZ, True, selmark, Nothing, 0)).
          If you replace selection line in your code by recorded line then macro should work

          Regards

            • Re: Select Origin for Autodimension Selected Drawing View Example (VBA)
              Steve Johnson

              Here's what I have, but it's still not working.  It either selects a different drawing view or it selects the default lower left corner as the origin.

              Even if I record a macro and replay it on the same drawing it does not work properly.

               

              Option Explicit

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swDrawing As SldWorks.DrawingDoc

              Dim swModelDocExt As SldWorks.ModelDocExtension

              Dim status As Boolean

              Dim fileName As String

              Dim errors As Long

              Dim warnings As Long

              Dim selmark As Long

              Dim ret As Long

              Sub main()

                  Set swApp = Application.SldWorks

                  ' Open drawing document of part

                  Set swModel = swApp.ActiveDoc

                  Set swDrawing = swModel

                  status = swDrawing.ActivateView("Drawing View1")

                  Set swModelDocExt = swModel.Extension

                  ' Select drawing view

                  status = swModelDocExt.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)

                  ' Horizontal and vertical datum, or a vertex datum, baselines for

                  ' dimension creation

                  ' These are optional; if not selected, autodimension uses default datums,

                  ' the leftmost and bottommost edges

                  'selmark = swAutodimMark_e.swAutodimMarkHorizontalDatum

                  'selmark = swAutodimMark_e.swAutodimMarkVerticalDatum

                  selmark = swAutodimMark_e.swAutodimMarkOriginDatum

                  ' Select a vertex

                  status = swModelDocExt.SelectByID2("Point1@Origin@***SW Model File Name***@Drawing View1", "EXTSKETCHPOINT", 0, 0, 0, True, 8, Nothing, 0)

                  ' Autodimensions the drawing view based on the selected vertex

                  ret = swDrawing.AutoDimension(swAutodimEntities_e.swAutodimEntitiesBasedOnPreselect, swAutodimScheme_e.swAutodimSchemeOrdinate, swAutodimHorizontalPlacement_e.swAutodimHorizontalPlacementAbove, swAutodimScheme_e.swAutodimSchemeOrdinate, swAutodimVerticalPlacement_e.swAutodimVerticalPlacementLeft)

              End Sub

                • Re: Select Origin for Autodimension Selected Drawing View Example (VBA)
                  Viktor Bovzdarenko

                  Hi Steve

                  Please look at this macro: #CODE|Select Origin in the Drawing View and insert dimension

                  There are 2 pause points for you to make the selections and continue running macro.

                  Debug.Assert False 'Select a drawing view

                  and

                  Debug.Assert False 'Select a point on drawing view

                  The macro will insert 2 ordinate dimension to selected point using: swDrawing.AutoDimension

                  But the behavior is not really consistent for Autodimension api. Maybe you should consider using: AddHorizontalDimension2 and AddVerticalDimension2.

                   

                  point 1 is selected by macro automatically. point 2 should be selected by user

                  result:

                  cheers

                    • Re: Select Origin for Autodimension Selected Drawing View Example (VBA)
                      Steve Johnson

                      Viktor,

                       

                      My ultimate goal is to paste this inside another macro that creates a drawing from a model.  The intention is to open model, run macro that creates drawing using specified drawing template and predefined drawing views, autodimension (ordinate) drawing view1, save and close drawing, save and close model.  Then I can batch this for all models that need drawings of a certain type, e.g. machined part, or weldment, etc.  However, in order for that to work the macro needs to be able to select Point1@Origin of the model in Drawing View1.

                       

                      Any help accomplishing this is greatly appreciated.

                        • Re: Select Origin for Autodimension Selected Drawing View Example (VBA)
                          Viktor Bovzdarenko

                          For selecting origin of the part from drawing view provided example works fine: #CODE|Select Origin in the Drawing View

                            • Re: Select Origin for Autodimension Selected Drawing View Example (VBA)
                              Steve Johnson

                              That works great for selecting the origin!  Now I'm struggling with how to fit that in with the rest of the code.  You'll have to forgive me, I'm very green when it comes to macros.

                               

                              Dim swApp As SldWorks.SldWorks

                              Dim swModel As SldWorks.ModelDoc2

                              Dim swSelMgr As SldWorks.SelectionMgr

                              Dim swModelDocExt As SldWorks.ModelDocExtension

                              Dim boolstatus As Boolean

                              Dim SelMark As Long

                              Dim ret As Long

                               

                               

                               

                              Sub main()

                               

                                  Set swApp = Application.SldWorks

                               

                                  Set swModel = swApp.ActiveDoc

                               

                                  Set swSelMgr = swModel.SelectionManager

                               

                                  Set swModelDocExt = swModel.Extension

                               

                                  Dim swView As SldWorks.View

                               

                                  Set swDrawing = swModel

                               

                                  boolstatus = swDrawing.ActivateView("Drawing View1")

                               

                                  boolstatus = swModelDocExt.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)

                               

                                  Set swView = swSelMgr.GetSelectedObject6(1, -1)

                                  

                               

                                  SelMark = swAutodimMark_e.swAutodimMarkOriginDatum

                               

                                      If Not swView Is Nothing Then

                                      Dim swRefDoc As SldWorks.ModelDoc2

                                   

                                      Set swRefDoc = swView.ReferencedDocument

                                   

                                      Dim swOrigin As SldWorks.Feature

                                      Set swOrigin = GetOriginFeature(swRefDoc)

                                   

                                      swView.SelectEntity swOrigin, False

                                  Else

                                      MsgBox "Please select view"

                                  End If

                               

                                  ret = swDrawing.AutoDimension(1, 2, 1, 2, -1)

                              End Sub

                               

                               

                               

                              Function GetOriginFeature(swModel As SldWorks.ModelDoc2) As SldWorks.Feature

                               

                                  Dim swFeat As SldWorks.Feature

                                  Set swFeat = swModel.FirstFeature

                                  While Not swFeat Is Nothing

                                      If swFeat.GetTypeName2() = "OriginProfileFeature" Then

                                          Set GetOriginFeature = swFeat

                                          Exit Function

                                      End If

                                      Set swFeat = swFeat.GetNextFeature

                                  Wend

                               

                              End Function