6 Replies Latest reply on Sep 9, 2013 3:36 AM by Matteo Orlandelli

    Export to dwg with layers named as each part of the assembly

    Luca Reggiani

      Hello to all,

      anyone knows a macro to export to dwg with a layers named as each part of the assembly?

        • Re: Export to dwg with layers named as each part of the assembly
          Glenn Schroeder

          Luca,

           

          Since you're needing a macro, you might want to move this discussion to the API section of the Forum (click on "Move discussion" in the Actions section at top right).  The macro people will be more likely to see it there.

          • Re: Export to dwg with layers named as each part of the assembly
            Deepak Gupta

            Do you have the each component view(s) placed on different layer??

              • Re: Export to dwg with layers named as each part of the assembly
                Luca Reggiani

                Glenn Schroeder ha scritto:

                 

                Luca,

                 

                Since you're needing a macro, you might want to move this discussion to the API section of the Forum (click on "Move discussion" in the Actions section at top right).  The macro people will be more likely to see it there.

                 

                Hi Glenn, honestly I do not know if you need a macro or other function, I would that after the export to dwg, assembly.dwg contained a layer for each part of the assembly.sldasm

                 

                 

                Deepak Gupta ha scritto:

                 

                Do you have the each component view(s) placed on different layer??

                 

                Hi Gupta, I did not understand your question (because of my bad English).

                I try to explain with an example:

                I drew the third part (shaft.sldprt - bearing.sldprt - support.sldprt).

                Now I create an assembly (assembly.sldasm) that contains the 3 part.

                Now I create the table (assembly.slddrw) with 3 views (plant - sectional view - side view).

                 

                I would like to export to dwg, and when I open "assembly.dwg", I want to find:

                layer "shaft"

                layer "bearing"

                layer "support"

                 

                So I can easily select all lines (circles, arcs, ..) that make up the "shaft" or any other component.

                 

                How can I have this?

                  • Re: Export to dwg with layers named as each part of the assembly
                    Matteo Orlandelli

                    You have to work on SolidWorks layers befor to export the dwg.

                    Manually the procedure is the following:

                    Create the layers you need then, in the main view of your drawing, select each component, right click and select "component line style" (under component section). Here deselect the flag "Use default" and on the bottom side apply the matching layer to the component ...

                    Your dwg export will have different layer for each component ...

                     

                    Of course this should be done by a macro (specially if your assembly has high comp number), and I think it's not too difficult to realize it ... I haven't time now, but maybe you can find someone here who already did something similar ... or maybe you want to learn programming SolidWorks and this is a good exercise!

                    good luck

                      • Re: Export to dwg with layers named as each part of the assembly
                        Luca Reggiani

                        Ciao Matteo

                        Matteo Orlandelli ha scritto:

                         

                        You have to work on SolidWorks layers befor to export the dwg.

                        Manually the procedure is the following:

                        Create the layers you need then, in the main view of your drawing, select each component, right click and select "component line style" (under component section). Here deselect the flag "Use default" and on the bottom side apply the matching layer to the component ...

                        Your dwg export will have different layer for each component ...

                         

                        ok, I understood how manually gets to create new layers and assign parts or sub assemblies to the new levels.

                        (here is a video that shows how to ... http://youtu.be/8KLwFFoXJeg )

                        Matteo Orlandelli ha scritto:

                         

                        Of course this should be done by a macro (specially if your assembly has high comp number), and I think it's not too difficult to realize it ... I haven't time now, but maybe you can find someone here who already did something similar ... or maybe you want to learn programming SolidWorks and this is a good exercise!

                        good luck

                         

                        But I work with assemblies with many parts and I can not think of doing this myself, so I asked for help here.

                        My problem is that I do not know VBA, I have tried to record a macro, but the problem is how to transform the recorded macro in a macro that works well ...

                        The problem is to create a list of all the names of the parties, create as many layers and then assign each part to their level ...

                         

                         

                        I hope that some of you know how to do or I hope someone have already found a similar macro ... I stay listening

                          • Re: Export to dwg with layers named as each part of the assembly
                            Matteo Orlandelli

                            Ciao Luca,

                            I just found on the API help exactly what you need!

                            "Put Assembly Components in Drawing View on Different Layers Example (VBA)"

                             

                            Tested and working. The only drawback is that it applies only to the selected view ... I don't know if there is a way to directly apply to all views in the drawing.

                             

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

                             

                            '

                             

                            ' Preconditions:

                             

                            '       (1) Drawing document is open.

                             

                            '       (2) Drawing view of an assembly is selected.

                             

                            '

                             

                            ' Postconditions:

                             

                            '       (1) New drawing layer is created for each component.

                             

                            '       (2) Each part is put on its own layer.

                             

                            '

                             

                            ' NOTE: Illegal characters are replaced with legal characters when creating a new

                             

                            '        drawing layer.

                             

                            '

                             

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

                             

                            Option Explicit

                             

                             

                             

                            Public Enum swLineStyles_e

                             

                                swLineCONTINUOUS = 0

                             

                                swLineHIDDEN = 1

                             

                                swLinePHANTOM = 2

                             

                                swLineCHAIN = 3

                             

                                swLineCENTER = 4

                             

                                swLineSTITCH = 5

                             

                                swLineCHAINTHICK = 6

                             

                            End Enum

                             

                             

                             

                            Public Enum swLineWeights_e

                             

                                swLW_NONE = -1

                             

                                swLW_THIN = 0

                             

                                swLW_NORMAL = 1

                             

                                swLW_THICK = 2

                             

                                swLW_THICK2 = 3

                             

                                swLW_THICK3 = 4

                             

                                swLW_THICK4 = 5

                             

                                swLW_THICK5 = 6

                             

                                swLW_THICK6 = 7

                             

                                swLW_NUMBER = 8

                             

                                swLW_LAYER = 9

                             

                            End Enum

                             

                             

                             

                            Private Sub ChangeComponentLayer(swApp As SldWorks.SldWorks, swDraw As SldWorks.DrawingDoc, swDrawComp As SldWorks.DrawingComponent, sLayerName As String)

                             

                                Dim bRet                    As Boolean

                             

                             

                             

                                ' Form a legal layer name by replacing backslash (/) and at sign (@) symbols

                             

                                ' with underscores

                             

                                sLayerName = Replace(sLayerName, "/", "_")

                             

                                sLayerName = Replace(sLayerName, "@", "_")

                             

                             

                             

                                bRet = swDraw.CreateLayer(sLayerName, "layer for " & sLayerName, 0, swLineCONTINUOUS, swLW_NORMAL, True): Debug.Assert bRet

                             

                             

                             

                                swDrawComp.Layer = sLayerName

                             

                            End Sub

                             

                             

                             

                            Sub ProcessDrawingComponent(swApp As SldWorks.SldWorks, swDraw As SldWorks.DrawingDoc, swDrawComp As SldWorks.DrawingComponent, sPadStr As String)

                             

                                Dim vDrawCompChildArr           As Variant

                             

                                Dim vDrawCompChild              As Variant

                             

                                Dim swDrawCompChild             As SldWorks.DrawingComponent

                             

                             

                             

                                Debug.Print sPadStr & swDrawComp.Name

                             

                             

                             

                                ChangeComponentLayer swApp, swDraw, swDrawComp, swDrawComp.Name

                             

                             

                             

                                vDrawCompChildArr = swDrawComp.GetChildren

                             

                                If Not IsEmpty(vDrawCompChildArr) Then

                             

                                    For Each vDrawCompChild In vDrawCompChildArr

                             

                                        Set swDrawCompChild = vDrawCompChild

                             

                             

                             

                                        ProcessDrawingComponent swApp, swDraw, swDrawCompChild, sPadStr + "  "

                             

                                    Next

                             

                                End If

                             

                            End Sub

                             

                             

                             

                            Sub main()

                             

                                Dim swApp                       As SldWorks.SldWorks

                             

                                Dim swModel                     As SldWorks.ModelDoc2

                             

                                Dim swDraw                      As SldWorks.DrawingDoc

                             

                                Dim swSelMgr                    As SldWorks.SelectionMgr

                             

                                Dim swView                      As SldWorks.View

                             

                                Dim swDrawComp                  As SldWorks.DrawingComponent

                             

                                Dim bRet                        As Boolean

                             

                                Set swApp = Application.SldWorks

                             

                                Set swModel = swApp.ActiveDoc

                             

                                Set swDraw = swModel

                             

                                Set swSelMgr = swModel.SelectionManager

                             

                                Set swView = swSelMgr.GetSelectedObject5(1)

                             

                                Set swDrawComp = swView.RootDrawingComponent

                             

                             

                             

                                Debug.Print "File = " & swModel.GetPathName

                             

                                Debug.Print "  " & swView.Name & "  [" & swView.Type & "]"

                             

                             

                             

                                ProcessDrawingComponent swApp, swDraw, swDrawComp, "    "

                                swApp.SendMsgToUser "Fatto!"

                             

                            End Sub

                             

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