17 Replies Latest reply on Feb 6, 2014 10:18 AM by Chris Johnson

    Open Part Model associated with Titleblock

    Chris Johnson

      Hi All,

       

      I have a question for you.

       

      How would I go about determining which drawing view is associated with the title block and then open that part/assy?

       

      basically, I've written a macro that made some changes to a drawing and now i want to open the part/assy model and make some changes to that also, but I don't know how.

       

      I recorded a macro to try and figure it out but, I'm thinking there must be a cleaner way to get this done.

       

      it is important that the macro doesn't care if its a part or assy model because this macro will be used for both.

       

      thanks in advance for your help!

       

      Chris

        • Re: Open Part Model associated with Titleblock
          Deepak Gupta

          Do you have different part/assy views in the same drawing?? If not then you can use the first view in the drawing to get/open the referenced model.

           

          Check for Get Document Referenced by Drawing View (VBA) example in SW API help files.

          • Re: Open Part Model associated with Titleblock
            Matt Martens

            Dim swSheet as SldWorks.Sheet

            Dim swView as SldWorks.View

            Set swSheet = swModel.GetCurrentSheet

            Set swView = swModel.GetFirstView

             

            If swSheet.CustomPropertyView = "Default" Then

                Set swView = swView.GetNextView

            Else

                Do While Not swView.Name = swSheet.CustomPropertyView

                    Set swView = swView.GetNextView

                Loop

            End If

             

            Dim swModel as SldWorks.ModelDoc2

            Set swModel = swView.ReferencedDocument


            The above code will look at the sheet properties and determine which drawing view is associated with the title block properties and then return the model doc object for the model in that view.

              • Re: Open Part Model associated with Titleblock
                Chris Johnson

                Matt,

                 

                This helps alot!

                once I figure out how to open the model after I get the firstview I should be good to go.

                 

                Thanks

                Chris

                  • Re: Open Part Model associated with Titleblock
                    Matt Martens

                    Once you get the ModelDoc2 object retrieve the document filename using IModelDoc2::GetPathName and the type using IModelDoc2::GetType and pass them to one of the ISldWorks::OpenDoc methods.

                      • Re: Open Part Model associated with Titleblock
                        Chris Johnson

                        Got it, thanks for the help!!

                         

                        Chris

                          • Re: Open Part Model associated with Titleblock
                            Chris Johnson

                            Matt,

                            with your help from yesterday, I have cobbled together some code that is supposed to print the name of the model in the drawing view  and then open that model.

                            it prints just fine but it wont open the model.

                             

                            Would you be kind enough to have a look and let me know if you see any problem?

                             

                            thank you,

                            Chris

                             

                                Option Explicit

                               

                                Sub main()

                                  

                                Dim swApp                     As SldWorks.SldWorks

                                Dim swModel                   As SldWorks.ModelDoc2

                                Dim swModView                 As SldWorks.ModelView

                                Dim swView                    As SldWorks.View

                                Dim swDraw                    As SldWorks.DrawingDoc

                                Dim nDocType                  As Long

                                Dim nErrors                   As Long

                                Dim nWarnings                 As Long

                                Dim sModelName                As String

                                Dim swSheet                   As SldWorks.Sheet

                                Dim sDocFileName              As String

                               

                                'Connect to Solidworks & active document

                                    Set swApp = Application.SldWorks

                                    Set swModel = swApp.ActiveDoc

                                    Set swSheet = swModel.GetCurrentSheet

                                    Set swView = swModel.GetFirstView

                                

                                    If swSheet.CustomPropertyView = "Default" Then

                                        Set swView = swView.GetNextView

                                    Else

                                        Do While Not swView.Name = swSheet.CustomPropertyView

                                        Set swView = swView.GetNextView

                                        Loop

                                    End If

                             

                                Set swModel = swView.ReferencedDocument

                               

                                sDocFileName = swModel.GetPathName

                             

                                    Debug.Print sDocFileName

                             

                               'Determine type of SolidWorks file based on file extension

                             

                                    If InStr(LCase(sDocFileName), "sldprt") > 0 Then

                                        nDocType = swDocPART

                                    ElseIf InStr(LCase(sDocFileName), "sldasm") > 0 Then

                                        nDocType = swDocASSEMBLY

                                    ElseIf InStr(LCase(sDocFileName), "slddrw") > 0 Then

                                        nDocType = swDocDRAWING

                                    Else

                                    ' Probably not a SolidWorks file...

                                    nDocType = swDocNONE

                                    '...so the file cannot be opened

                                    Exit Sub

                                End If

                             

                                Set swModel = swApp.OpenDoc6(sDocFileName, nDocType, swOpenDocOptions_Silent, "", nErrors, nWarnings)

                             

                            End Sub