6 Replies Latest reply: Feb 6, 2013 8:13 PM by Artem Taturevych

    ASSEMBLY CHECK

    Jay Donihoo

      Looking for insight on checking if an assembly is open from drawing, then either opening or switching to the assembly.

        • Re: ASSEMBLY CHECK
          Keith Rice

          Jay,

           

          If the drawing that references an assembly is open (and the drawing is not a detached drawing) then the assembly is already open. You can use ISldWorks::ActivateDoc3 to activate an already open document.

           

          For example, if you a single drawing open in SolidWorks (nothing else active, if you go the Windows dropdown), and that drawing references an assembly, this code will activate that assembly:

          Dim swApp As SldWorks.SldWorks

          Dim vDocs As Variant

          Dim swModel As SldWorks.ModelDoc2

          Dim i As Integer

           

          Sub main()

              Set swApp = Application.SldWorks

              vDocs = swApp.GetDocuments

              For i = 0 To UBound(vDocs)

                  Set swModel = vDocs(i)

                  If Right(UCase(swModel.GetTitle), 6) = "SLDASM" Then swApp.ActivateDoc3 swModel.GetTitle, True, 1, Empty

              Next i

          End Sub

           

          Keith

          Video Tutorials for the SolidWorks API

          • Re: ASSEMBLY CHECK
            Artem Taturevych

            Not only the detached drawing but lightweight components are not opened in memory. Furthermore there is no any issue calling the OpenDoc6 prior to activate. If document is loaded just nothing happens otherwise it will be loaded.

             

            Below is the macro which finds the referenced assembly of drawing rather than any assembly opened in SolidWorks. I'm not sure what exactly you are looking for but Keith's example will activate any assembly opened in SolidWoks without the check whether it is associated with current drawing.

             

            I would also suggest to use Right function to check the extension rather than InStr. Because if the file name contains SLDASM this will be recognized as assembly (mostly unlikely but still the possibility).

             

            Dim swApp As SldWorks.SldWorks

            Dim swDraw As SldWorks.DrawingDoc

             

            Sub main()

             

                Set swApp = Application.SldWorks

               

                Set swDraw = swApp.ActiveDoc

               

                Dim vSheets As Variant

                vSheets = swDraw.GetViews

               

                Dim i As Integer

                Dim j As Integer

               

                For i = 0 To UBound(vSheets)

                    Dim vViews As Variant

                    vViews = vSheets(i)

                    For j = 0 To UBound(vViews)

                        Dim swView As SldWorks.View

                        Set swView = vViews(j)

                        Dim modelPath As String

                        modelPath = swView.GetReferencedModelName

                        If LCase(Right(modelPath, 6)) = "sldasm" Then

                            Dim swRefModel As SldWorks.ModelDoc2

                            Set swRefModel = swApp.OpenDoc6(modelPath, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", 0, 0)

            swApp.ActivateDoc3 swRefModel.GetTitle, False, 0, 0

                            End

                        End If

                    Next

                Next

               

            End Sub

            __________________________

            Regards,

            Artem Taturevych

            Application Engineer at Intercad

            http://intercad.com.au/

            Tel: +61 2 9454 4444

              • Re: ASSEMBLY CHECK
                Keith Rice

                Thanks for noting that Right() is better than Instr(), though I only presented that code to prove the point that an assembly is already open if a drawing that references it is open. If we're going to write him an error-proof macro, then we should also be using IModelDoc2::GetPathName instead of IModelDoc2::GetTitle, since the latter only returns the extension if "Hide extensions for known file types" is turned off in the Windows folder options.

                 

                Keith

                Video Tutorials for the SolidWorks API

                  • Re: ASSEMBLY CHECK
                    Artem Taturevych

                    Keith,

                     

                    I do not say that your macro is incorrect - I just described the difference of my macro and yours and possible 'bottlenecks' in first example. As I said it is not completely clear to me what is requested. Perhaps your macro is exactly what he is looking for, but may be not.

                     

                    ActivateDoc3 will work with the GetTitle regardless this option checked on or off:

                     

                    This will activate the assembly in both cases:

                     

                    swApp.ActivateDoc3 swRefModel.GetTitle, False, 0, 0

                      • Re: ASSEMBLY CHECK
                        Keith Rice

                        I modified my code so that it uses Right() instead of Instr() and also takes into account upper/lower case. Nevertheless it really does not accomplish much at all, it really just proves that ActivateDoc3 will activate an assembly being referenced by an open drawing. Detached and lightweight drawings are exceptions to this, of course.

                         

                        Concerning IModelDoc2::GetTitle... it also has a bottleneck. If multiple documents are open with the same name and "Hide extensions for known file types" is turned on then when used with ActivateDoc3 it can result in the wrong document being activated.

                         

                        Keith

                        Video Tutorials for the SolidWorks API

                          • Re: ASSEMBLY CHECK
                            Artem Taturevych

                            Keith,

                             

                            Try the following scenario:

                             

                            1) Open any drawing which refers to any assembly

                            2) Open another assembly (not the one which is the reference in the drawing)

                            3) Activate drawing and run your macro. You will see that this will activate both assemblies one-by-one. And the last assembly will be the one from step #2 because it appears later in the documents list but not the one referenced by the drawing.

                            4) Then try my macro - it will activates only the assembly which referenced by the drawing.

                             

                            And this is the main difference between these two macros. The way of activating is not important.

                            __________________________

                            Regards,

                            Artem Taturevych

                            Application Engineer at Intercad

                            http://intercad.com.au/

                            Tel: +61 2 9454 4444