13 Replies Latest reply on Nov 7, 2013 10:17 AM by Elmar Klammer

    How to use "Update Standard Views" in SW 2013 API?

    Laszlo Szucs

      Hi,

       

      I would like to use the enclosed macro in SW 2013 but it crashes all the time.

      The "UpdateStandardViews" method doesn't exist in the SW 2013 API but maybe

      there is a way to implement it somehow. I tried to copy the SW 2014 reference files

      (type libraries etc...) into the macro location that helped me in some cases in the past

      but it doesn't work now.

       

      The task would be to create standard 3 views automatically about different bodies of

      a multibody part based on the user's face selection and put all the views on one sheet.

      I thought it is a good idea to use the "normal to" command to turn each face into the

      right direction one by one and make a "Front" view out of this so the standard 3 views

      creation would be quite simple. (Of course the user has to take care of selecting the

      right surfaces for the front views.)

       

      Does anybody have an idea how to do this in SW 2013?

       

      Thanks in advance for your help!

      Laszlo

        • Re: How to use "Update Standard Views" in SW 2013 API?
          Simon Turner

          This was only added in 2014 I' afraid: UpdateStandardViews Method (IModelDocExtension)

          I don't think it is possible to edit the standard views via the API prior to 2014.

          You can create your own views though - that might be a workaround.


            • Re: How to use "Update Standard Views" in SW 2013 API?
              Laszlo Szucs

              Yes, this method was only added in 2014 but I need to do

              something similar (in best case the same) in 2013.

               

              Creating own views sounds a good idea but I don't know

              how to do it on the quickest and easiest way.

              (The problem is not the creation of the own views only but

              putting them on one sheet one by one.)

                • Re: How to use "Update Standard Views" in SW 2013 API?
                  Simon Turner

                  The first thing to do is to get the view you want. I use ModelView::Orientation3 myself, but that does require a bit of work to calculate the MathTransform. Alternatively you could use RunCommand(swCommands_View_Normal_To) assuming that you have selected a flat face or plane to view normal to.

                  Next use ModelDoc2::NameView

                  Finally use CreateDrawViewFromModelView3 to insert that Named View into the 2D drawing.

                    • Re: How to use "Update Standard Views" in SW 2013 API?
                      Laszlo Szucs

                      I used swModel.ShowNamedView2 "*Normal To", -1 to get the view I want (Face was selected).

                      The NameView and CreateDrawViewFromModelView3 are very useful so thanks for that but I faced

                      the next problem: I work with multibody parts and some of the bodies are sheet metals and others not.

                      If the selected face belongs to a sheet metal I have to create a flat pattern view only but I can't do

                      that with CreateFlatPatternViewFromModelView3 because all the other bodies are in the view.

                       

                      I suppose there should be two different ways to solve this:

                      1. Select the required sheet metal body and use only this one to create the flat pattern view

                      2. Create a view with all the bodies. Select the required sheet metal body and turn the view

                      to flat pattern view somehow.

                       

                      I tried both but no success... Probably I tried to use wrong functions or methods.

                      Any idea?

                       

                      Thanks.

                        • Re: How to use "Update Standard Views" in SW 2013 API?
                          Simon Turner

                          For multi-body views, you have to select the body from the ModelDoc2 representing the part file.

                          From some old code that I wrote a long time ago, you need to select the flat face on the model, marking it as "1".

                          Also select the body itself with mark "4"

                          Then I think that CreateFlatPatternViewFromModelView3 should work (although I used CreateRelativeView in my old code).

                           

                          Here is my old code:

                           

                           

                              Set myEntity = myFace

                              Set selMgr2 = myDoc.SelectionManager 'myDoc is the ModelDoc2 for the 3d part

                              Set selData = selMgr2.CreateSelectData

                              selData.Mark = 1

                              myEntity.Select4 False, selData

                              myDoc.Extension.SelectByID2 myBody.Name, "SOLIDBODY", 0, 0, 0, True, 4, Nothing, 0

                             

                              Set Draw = swApp.ActivateDoc2(Draw.GetTitle, True, errors) 'switch back to the 2D drawing

                              Set myDraw = Draw 'DrawingDoc object

                              Set myView = myDraw.CreateRelativeView(myDoc.GetPathName, x, y, 0, swRelativeViewCreationDirection_e.swRelativeViewCreationDirection_TOP)

                            • Re: How to use "Update Standard Views" in SW 2013 API?
                              Laszlo Szucs

                              Your solution works fine, thank you very much! Here is my updated code based on your one:

                              (One face has to be selected in a multibody part.)

                               

                              Dim swApp As SldWorks.SldWorks

                              Dim swModel As SldWorks.ModelDoc2

                              Dim selMgr As SldWorks.SelectionMgr

                              Dim selFace As SldWorks.Face2

                              Dim selData As SldWorks.SelectData

                              Dim BoolStatus As Boolean

                              Dim selEntity As SldWorks.Entity

                              Dim selBody As SldWorks.Body2

                              Dim swDrawing As SldWorks.DrawingDoc

                              Dim swView As SldWorks.View

                               

                              Sub main()

                               

                                  Set swApp = Application.SldWorks

                                  Set swModel = swApp.ActiveDoc

                                  Set selMgr = swModel.SelectionManager

                                  Set selFace = selMgr.GetSelectedObject6(1, -1)

                                  Set selBody = selFace.GetBody

                                  Set selData = selMgr.CreateSelectData

                               

                                  Set selEntity = selFace

                               

                                  selData.Mark = 1

                               

                                  BoolStatus = selEntity.Select4(False, selData)

                               

                                  swModel.Extension.SelectByID2 selBody.Name, "SOLIDBODY", 0, 0, 0, True, 4, Nothing, 0

                               

                                  Set swDrawing = swApp.NewDocument(swApp.GetUserPreferenceStringValue(swDefaultTemplateDrawing), 12, 1.188, 0.84)

                                  Set swView = swDrawing.CreateFlatPatternViewFromModelView3(swModel.GetPathName, "", 0, 0, 0, False, False)

                               

                              End Sub

                              • Re: How to use "Update Standard Views" in SW 2013 API?
                                Elmar Klammer

                                HELLO SIMON,

                                 

                                I can't get the create-relative view call to work. The code runs the swView always ends up "empty" and no view is actually created.

                                Can you post the code for you old makro?

                                 

                                Thanks,

                                 

                                Elmar

                                  • Re: How to use "Update Standard Views" in SW 2013 API?
                                    Simon Turner

                                    I did post my old code above. The rest is specific to a customer project, so I cannot upload the full project. Sorry.

                                      • Re: How to use "Update Standard Views" in SW 2013 API?
                                        Elmar Klammer

                                        wHello Simon

                                         

                                        I have modified the code and tried to make it work. The idea is to have the part open and preselect a face of a multibody.

                                        The macro then opens an existing drawing (same name as part and inserts a relative view). When i run the code it switches from the part to the drawing but the swView comes up as "nothing" and no relativeview is created.

                                         

                                         

                                        Set swView = swDraw.CreateRelativeview(component, 0.1, 0.01, swRelativeViewCreationDirection_e.swRelativeViewCreationDirection_FRONT, swRelativeViewCreationDirection_e.swRelativeViewCreationDirection_RIGHT)

                                         

                                         

                                        I am not a programmer and would appriciate  any pointers on how to solve this issue. The existing code i have take from this and other posts and tried to make it work.

                                         

                                         

                                        Here is the main macro:

                                         

                                         

                                        Dim swApp As SldWorks.SldWorks

                                        Dim swDraw As SldWorks.DrawingDoc

                                        Dim swModel As SldWorks.ModelDoc2

                                         

                                         

                                        Dim component, drawingname, name As String

                                         

                                        Sub main()

                                         

                                            Set swApp = Application.SldWorks

                                           '____

                                         

                                        'Set swApp = Application.SldWorks

                                        Set swModel = swApp.ActiveDoc

                                        Set selMgr = swModel.SelectionManager

                                        Set selFace = selMgr.GetSelectedObject6(1, -1)

                                        Set selBody = selFace.GetBody

                                        Set selData = selMgr.CreateSelectData

                                         

                                        Set selEntity = selFace

                                        selData.Mark = 1

                                         

                                        BoolStatus = selEntity.Select4(False, selData)

                                         

                                        swModel.Extension.SelectByID2 selBody.name, "SOLIDBODY", 0, 0, 0, True, 4, Nothing, 0

                                        component = swModel.GetPathName

                                        Call opendrw.main 'opens drawing of part file

                                        Set swDraw = swApp.ActiveDoc

                                        drawingname = swDraw.GetTitle 'Left(component, Len(component) - 7) & ".slddrw"

                                        swApp.ActivateDoc3 drawingname, False, 0, 0

                                        name = component

                                        Dim swView As SldWorks.View

                                        Set swView = swDraw.CreateRelativeview(component, 0.1, 0.01, swRelativeViewCreationDirection_e.swRelativeViewCreationDirection_FRONT, swRelativeViewCreationDirection_e.swRelativeViewCreationDirection_RIGHT)

                                         

                                        End Sub

                                         

                                         

                                        Here is the Opendrw module:

                                         

                                        Option Explicit

                                         

                                        Dim swApp As SldWorks.SldWorks

                                        Dim swModel As ModelDoc2

                                         

                                        Dim DocName As String

                                        Dim DrwName As String

                                         

                                        Dim swLoadErrors As Long

                                        Dim swLoadWarnings As Long

                                         

                                        Sub main()

                                         

                                        Set swApp = Application.SldWorks

                                         

                                        Set swModel = swApp.ActiveDoc

                                         

                                        DocName = swModel.GetTitle

                                         

                                        DrwName = Left$(DocName, Len(DocName) - 7) & ".slddrw"

                                         

                                        DrwName = Replace(swModel.GetPathName, DocName, DrwName, 1, , vbTextCompare)

                                         

                                        If Dir$(DrwName) <> "" Then

                                         

                                        Set swModel = swApp.OpenDoc6(DrwName, swDocDRAWING, swOpenDocOptions_Silent, "", swLoadErrors, swLoadWarnings)

                                        'swApp.ActivateDoc3 swModel.GetTitle, True, 0, 0

                                        End If

                                         

                                        End Sub

                                          • Re: How to use "Update Standard Views" in SW 2013 API?
                                            Simon Turner

                                            For CreateRelativeView, you actually need 2 faces selected. The first one is the one you will look onto. The second one defines the horizontal direction. I think it needs to be perpendicular to the first one.

                                             

                                            Here is a modifed version of your macro:

                                             

                                            Option Explicit

                                            Option Compare Text

                                             

                                            Dim swApp As SldWorks.SldWorks

                                            Dim swDraw As SldWorks.DrawingDoc

                                            Dim swModel As SldWorks.ModelDoc2

                                            Dim component As String, drawingname As String

                                            Dim BoolStatus As Boolean

                                             

                                            Sub main()

                                                Dim selFace1 As Face2

                                                Dim selFace2 As Face2

                                                Dim selEntity As Entity

                                                Dim selBody As Body2

                                                Dim selData As SelectData

                                                Dim selMgr As SelectionMgr

                                                Dim swLoadErrors As Long

                                                Dim swLoadWarnings As Long

                                                Dim swView As SldWorks.View

                                               

                                                Set swApp = Application.SldWorks

                                               

                                                Set swModel = swApp.ActiveDoc

                                                Set selMgr = swModel.SelectionManager

                                               

                                                Set selFace1 = selMgr.GetSelectedObject6(1, -1)

                                                If selMgr.GetSelectedObjectCount2(-1) > 1 Then

                                                    Set selFace2 = selMgr.GetSelectedObject6(2, -1)

                                                End If

                                                Set selBody = selFace1.GetBody

                                                Set selData = selMgr.CreateSelectData

                                                Set selEntity = selFace1

                                                selData.Mark = 1

                                                BoolStatus = selEntity.Select4(False, selData)

                                               

                                                If Not selFace2 Is Nothing Then

                                                    Set selData = selMgr.CreateSelectData

                                                    Set selEntity = selFace2

                                                    selData.Mark = 2

                                                    BoolStatus = selEntity.Select4(True, selData)

                                                End If

                                               

                                                BoolStatus = swModel.Extension.SelectByID2(selBody.name, "SOLIDBODY", 0, 0, 0, True, 4, Nothing, 0)

                                               

                                                component = swModel.GetPathName

                                                drawingname = Replace(component, ".sldprt", ".slddrw")

                                               

                                                Set swDraw = swApp.OpenDoc6(drawingname, swDocDRAWING, swOpenDocOptions_Silent, "", swLoadErrors, swLoadWarnings)

                                                Set swDraw = swApp.ActivateDoc3(drawingname, False, swRebuildOnActivation_e.swDontRebuildActiveDoc, swLoadErrors)

                                                Set swView = swDraw.CreateRelativeView(component, 0.1, 0.01, swRelativeViewCreationDirection_e.swRelativeViewCreationDirection_FRONT, swRelativeViewCreationDirection_e.swRelativeViewCreationDirection_TOP)

                                            End Sub