18 Replies Latest reply on Nov 2, 2016 8:12 AM by Nilesh Patel

    Api - change reference configuration in drawing

    Marco Nawijn

      I have an assembly with multiple configurations. I have also the drawing of the default configuration with a couple of views (of the parts) and also a isometric view of the whole assembly. When I change the configuration in the assembly file, the parts in the drawing change too (they get other dimensions) except the isometric view. To have this updated, I must do it manually, activating the respectively view and changing the reference configuration.

      I would like to create a macro that changes this configuration. It would be part of a bigger macro that generates drawings for each configuration of the assembly.

      Strange is that if a try to record a macro with the process of changing the reference configuration and I run it then, it will not work. What I noticed is that in the recorded macro is used the method ChangeRefConfigurationOfFlatPatternView...which doesn't work.

      Did you guys struggled with something similar?

        • Re: Api - change reference configuration in drawing
          Nilesh Patel

          Hi Marco,

           

          If you create the drawing views of parts in the same drawing sheet where you have an assembly view, it should not change the configuration when you change the configuration of the assembly in the drawing view. These parts' drawing views are independent from the assembly view. Could you attach some screen shots to give us more information?

           

          Regards,

            • Re: Api - change reference configuration in drawing
              Marco Nawijn

              Patel,

              All elements in the assembly are parametrically defined. The hole assembly is driven by a sketch. In a design table, the sketch gets different shapes (values for the sides) that implies different configurations for the assembly. That means that the parts in the assembly don't have configurations, they update automatically from one configuration of the assembly to another. This also means that the drawing of one assembly configuration contains the updated parts for that configuration. The only view that doesn't update is the isometric view of the assembly. For this I would like to write a macro that changes the reference configuration of the assembly in that particular view.

                • Re: Api - change reference configuration in drawing
                  Nilesh Patel

                  Hi Marco,

                   

                  Try attached macro. I have tried it and works OK for me (using Solidworks 2016, SP3).

                   

                  Save the drawing document with any configuration and then run the macro (Assembly model doesn't have to be open in SolidWorks). It saves the drawings as assembly configuration names into same folder where you have saved the first drawing. I have made an assumption that the drawing document has only one assembly view as you have shown in the screen shot.

                   

                  Let me know how you go.

                   

                  Regards,

                    • Re: Api - change reference configuration in drawing
                      Marco Nawijn

                      Hi Nilesh,

                      Before reading your macro, I wanted to ask you if you know why sometimes a macro that I make cannot open anymore (for editing). For example, the macro in the attachment...can you open it? Can you tell me why I cannot edit it anymore? If I try to edit it, the program(SW) freezes.

                      • Re: Api - change reference configuration in drawing
                        Marco Nawijn

                        Nilesh,

                        I run your macro and it seems to do his work great. The only problem is that the different drawing try to update wrt the assembly drawing (in which only one configuration is active). The result is that, when you open the saved drawings generated by the macro, the assembly is totally damaged. I cannot figure out if this is a problem at saving (for example the drawings should be saved as detached drawings) or at open the drawing (here it can automatically try to update the drawing). Is there an api command that saves the drawings as detached?

                          • Re: Api - change reference configuration in drawing
                            Nilesh Patel

                            Hi Marco,

                             

                            1. Detached drawing:

                            Replace following line "bSave = swModel.Extension.SaveAs(dwgPath + vConfigNames(i) + ".SLDDRW", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, lErrors, lWarnings)"  with this line "bSave = swModel.Extension.SaveAs(dwgPath + vConfigNames(i) + ".SLDDRW", swSaveAsVersion_e.swSaveAsDetachedDrawing, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, lErrors, lWarnings)".

                            This will save-as detached drawing.

                             

                            2. Missing Reference Library:

                            I tried to open your macro and had the same problem. My SolidWorks freeze too. One of the reason for missing library is the location of the reference library. Person who created the macro may have reference libraries located in different place than your machine. So when you try to open the macro, it starts looking for the reference libraries and because libraries are located in different place, macro cannot find them and therefore SW freezes. As Deepak mentioned, best practice should be to copy the codes in text file.

                             

                            3. ChangeRefConfigurationOfFlatPatternView:

                            I have tried this method and when I run the test macro, it creates derived default flat pattern configuration "configuration name-SM-FLAT-PATTERN' in the part and activates that derived flat pattern configuration in the drawing drawing. If you already have default SM-FLAT-PATTERN configuration in the part, then it will just activate it in the drawing view. If you want to change the reference configuration, then try ReferencedConfiguration Property (IView) as described by Peter.

                             

                            4. Learning Solidworks API:

                            If would like to learn SolidWorks API, try Keith Rice's website www.cadsharp.com. He has lots of tutorials and macros on this website. I have learned from him.

                             

                            Hope this helps.

                             

                            Regards,

                              • Re: Api - change reference configuration in drawing
                                Marco Nawijn

                                Nilesh,

                                I already tried to change the option for the save command, just as you said. But when I open the drawings I can see that the references are not broken, so the link still exist with the assembly file. I think the drawing must be closed after changing the configuration in the views, and then reopened again. So, there are some little modification to be made in the macro of Peter. Can you confirm this?

                                Related to the missing reference library, I created the macro and it would not open on my own machine. And it is not the first macro that, after a period, cannot be edited again. 

                                  • Re: Api - change reference configuration in drawing
                                    Nilesh Patel

                                    Hi Marco,

                                     

                                    Try attached. It closes the detached drawing as you mentioned and reopens the default drawing (the first drawing that was originally open) and loops until all configuration drawings are saved. I can see the broken link in the drawing document and views. I know about detached drawing but have never used it before, so not sure this is exactly what you want. Let me know how you go.

                                     

                                    I have never come across a situation where my own macro gets corrupted, so not sure why this happens.

                                     

                                    Good luck.

                                     

                                    Regards,

                                      • Re: Api - change reference configuration in drawing
                                        Marco Nawijn

                                        Nilesh, it's perfect, you're a genius. And you learned only from Keith Rice, or did you use other resources too?

                                        One little remark, the first configuration (the default) it's not saved as detached, so this one will try to update if I'll open it.

                                        I'm now studying your macro, try to learn from it... and hope that I'm not bothering you if i'll ask some questions.

                                        For example, the next sequence:

                                        dwgPath = swModel.GetPathName

                                                dwgName = dwgPath

                                                defaultDwg = dwgPath

                                                dwgPath = Left(dwgPath, Len(dwgPath) - (Len(dwgPath) - InStrRev(dwgPath, "\")))

                                                dwgName = Right(dwgName, Len(dwgName) - InStrRev(dwgName, "\"))

                                                dwgName = Left(dwgName, Len(dwgName) - (Len(dwgName) - InStrRev(dwgName, ".") + 1))

                                        I cannot identify the methods you've use here, namely Left, Right and InStrRev. Where did you get them?

                                          • Re: Api - change reference configuration in drawing
                                            Nilesh Patel

                                            Hi Marco,

                                             

                                            1. I used dwgPath to get the full path of the drawing document using swModel.GetPathName including the extension ".SLDDRW". Then converted this path into the folder path (where all other config drawings are saved) using 'Left' function. It is a VBA function. 'Len' gives you the length of the string. Just type 'string left VBA' in google and you will find plenty of examples to understand how it works.

                                            2. I created a full path of the configuration drawing using dwgPath + vConfigNames(i) + ".SLDDRW". You will see it in swModel.SaveAs... method.

                                            3. defaultDwg also gets the full path of the default drawing which you are using to save other configuration drawings. This variable is used in swApp.OpenDoc6.. to open same drawing every time.

                                            4. dwgName also gets the full path of the default drawing and then by using 'Left' and 'Right' VBA functions, I stored just a name of the default drawing (without any path or extension). There is a 'IF' condition in 'Sub main', 'If vConfigNames(i) <> dwgName' which checks the configuration name of the assembly against the drawing name and if the condition doesn't match then calls the procedure 'ProcessDocument' (which saves the configuration drawings as detached drawings). I thought you only wanted to save configuration drawings as detached drawing not the default drawing. If you would like to save all drawings as detached drawings then remove this if condition and the variable 'dwgName' (and any codes associated with this variable). This way all your drawings will be saved as detached drawings including the default drawing.

                                             

                                            I am not a professional programmer just an intermediate user, so someone may be able to reduce the line codes from my codes. Just put a break point in the beginning of the codes and run the codes line by line and you will get an idea of how it works. Let me know how you go and if you have any query, feel free to leave a comment or question.

                                             

                                            In regards to learning SolidWorks API, become a member on Keith Rice's website and you will see lots of example. Just study those examples and I am sure you will learn a lot. You can also use google, youtube etc. Solidworks offline API help is also a very good. There are plenty of examples there too. If you stuck somewhere, just ask a question on API forum and some will be more than happy to help you.

                                             

                                            Good luck.

                                             

                                            Regards,

                            • Re: Api - change reference configuration in drawing
                              Peter Brinkhuis

                              The reason your first solution doesn't work is in the name of the function itself. It isn't called ChangeRefConfigurationOfFlatPatternView for nothing, it should change the configuration of a flat pattern view.

                              2015 SOLIDWORKS API Help - ChangeRefConfigurationOfFlatPatternView Method (IDrawingDoc)

                               

                              I looked up the IView interface because I expected it to have a function to set the view configuration. It does:

                              2015 SOLIDWORKS API Help - ReferencedConfiguration Property (IView)

                               

                              I'm working on an add-in to partially automate drawing creation. Please send me a message if there's anything I can help you with.

                                • Re: Api - change reference configuration in drawing
                                  Marco Nawijn

                                  I know Peter, that command with the flat pattern was automatically generated when a tried to record a macro that changes the configuration in a view, to see what command can I use for this. Trying to run the macro afterwards it didn't do anything, of course. I'm a beginner in programming and it is difficult for me at this stage to identify the right commands for different actions. I'll keep you informed with my progress.