8 Replies Latest reply on Sep 18, 2014 10:21 AM by Dan Miel

    How to store user settiings for Solidworks custom addin/macro application?

    Cgtech Cgtech

      Hello All,

       

      I am making an Solidworks custom addin application, which contains GUI elements into which user interaction/choice is required.

       

      Now, I want to preserve all user selected settings of addin so next time when user opens up addin, all the previously selected settings can be loaded and user does not have re-select any of those.

       

      Can someone please guide me to group of APIs or libraries which can allow me to store all user settings for Solidworks custom addin application?

       

      Thanking in Advance,

      MD

        • Re: How to store user settiings for Solidworks custom addin/macro application?
          Edgar Quintana

          I guess you would have to store such selections in an external file (text file probably) an every time you run the add in it would read what ever is stored in the text file. Are you familiar with creating and reading text files?

            • Re: How to store user settiings for Solidworks custom addin/macro application?
              Cgtech Cgtech

              Hello Edgar,

               

              Thanks a lot for your reply,

               

              I was hoping that Solidworks may already be having a mechanism for that.

               

              But anyways, so I have to create my own reading-writing mechanism for external text file (XML file may be) right? If yes, then is there a place where it need to be located so Solidworks can pick-it up automatically each time addin being called.

               

              My adding will have different user settings for each Solidworks model on to which it is being called, so is there a way I can store user settings along with Solidworks model database?

               

              Again, thanks a lot for your help

                • Re: How to store user settiings for Solidworks custom addin/macro application?
                  Edgar Quintana

                  All this is going to depend on how you write your own Add-in.

                   

                  • You can specify the type of file (.txt, .xml, .xls, etc.)
                  • The location of such file (local drive, network drive)  can be anywhere you want it to be. It doesn't depend on solid works but for your add-in to have a path to read it.
                  • In resume you can store any data you want, and your code will have the necessary logic to read it and use it to fulfill its purpose.

                   

                   

                  Here's a code i wrote a long time ago to create text files. This may help you to do what you're trying to accomplish.

                   

                  Private Sub ImportTo_Click()

                  Dim FSO As FileSystemObject

                          Dim FSOFile As TextStream

                          Dim FilePath As String

                          Dim Create As String, DataSetPDF As String, PDFRunMe As String, SaveToFolder As String, result As String, Project As String

                         

                          Set swApp = Application.SldWorks

                          Set swModel = swApp.ActiveDoc

                          Set swDraw = swModel

                          Set swSheet = swDraw.GetCurrentSheet

                         

                          If swModel Is Nothing Or swDraw Is Nothing Then

                              swApp.SendMsgToUser "Unable to get SolidWorks Handle. Make sure a SolidWorks drawing is open"

                              Exit Sub

                          End If

                         

                          If Len(swModel.GetTitle) < 9 Then

                              swApp.SendMsgToUser "SolidWorks drawing file name has to be at least 9 digits (Ex. 123456700)"

                              Exit Sub

                          End If

                         

                         result = swApp.SendMsgToUser2("Are you sure you want to import drawing ?", swMbQuestion, swMbYesNo)

                        

                         If result = swMbHitNo Then

                              Exit Sub

                         Else

                              If result = swMbHitYes Then

                        

                                  If swSheet.GetSize(0, 0) = swDwgPaperDsize Then

                                      DrawType = "Top Level"

                                      DrawSize = "D"

                                  Else

                                      DrawType = "Sheet Metal"

                                      DrawSize = "B"

                                  End If

                             

                              SaveToFolder = "C:\Users\currentuser\Desktop\Test\"

                             

                              FileName = Left(swModel.GetTitle, 9)

                              Create = SaveToFolder & "Create" & FileName & ".txt"

                              DataSetPDF = SaveToFolder & "DataSetPDF" & FileName & ".txt"

                              Project = SaveToFolder & "Project" & FileName & ".txt"

                              PDFRunMe = SaveToFolder & "PDFRunMe" & FileName & ".cmd"

                             

                              If Revision = Empty Then

                                  Revision = InputBox("Please enter next letter revision (EX. A).")

                              End If

                                   

                     

                              Set FSO = New FileSystemObject

                              ' opens  file in write mode

                              Set FSOFile = FSO.OpenTextFile(Create, 2, True)

                              ' write your code here

                              FSOFile.WriteLine FileName & "|" & DrawType & "|" & DrawType & "|" & Revision & "|"

                              FSOFile.Close

                             

                              Dim ProjectNum As String

                              If DrawSize = "D" Then

                                  ProjectNum = Left(FileName, 7)

                                  Set FSO = New FileSystemObject

                                  ' opens  file in write mode

                                  Set FSOFile = FSO.OpenTextFile(Project, 2, True)

                                  ' write your code here

                                  FSOFile.WriteLine ProjectNum & "| /|" & FileName & "|"

                                  FSOFile.Close

                              Else

                                   ProjectNum = InputBox("Coil Number? (7 Digits, Ex. 1234567).")

                                   Set FSO = New FileSystemObject

                                  ' opens  file in write mode

                                  Set FSOFile = FSO.OpenTextFile(Project, 2, True)

                                  ' write your code here

                                  FSOFile.WriteLine ProjectNum & "| /|" & FileName & "|"

                                  FSOFile.Close

                              End If

                             

                              Set FSO = New FileSystemObject

                              Set FSOFile = FSO.OpenTextFile(DataSetPDF, 2, True)

                              ' write your code here

                              FSOFile.WriteLine "-f=" & FileName & DrawSize & Revision & ".pdf" & Space(1) & "-d=" & FileName & Space(1) & "-item=" & FileName _

                              & Space(1) & "-revision=" & Revision & Space(1) & "-type=PDF -ref=PDF -ie=y"

                              FSOFile.Close

                             

                              Set FSO = New FileSystemObject

                              Set FSOFile = FSO.OpenTextFile(PDFRunMe, 2, True)

                              ' write your code here

                              FSOFile.WriteLine "set T_ROOT=F:\" & vbCrLf _

                              & "set T_DATA=\\grd01\tdata" & vbCrLf _

                              & "call \\grd01\tdata\tc_profilevars.bat" & vbCrLf _

                              & "set TEMINSTALL=1" & vbCrLf _

                              & "F:" & vbCrLf _

                              & "cd F:\inetpub\wwwroot\PPSWeb\PDFImport\test\" & vbCrLf _

                              & "call CreateItems_83 -u=infodba -p=infodba -g=dba -sl=ok" & FileName & ".txt" & Space(1) & "-el=notok" & FileName & ".txt" & Space(1) & "-f=Create" & FileName & ".txt" & vbCrLf _

                              & "call FileUpload_83 -u=infodba -p=infodba -g=dba -i=DataSetPDF" & FileName & ".txt" & Space(1) & "-l=log" & FileName & "redo.log" _

                              & Space(1) & "-e=log" & FileName & ".log" & Space(1) & "-f=F:\inetpub\wwwroot\PPSWeb\PDFImport\test\" & vbCrLf _

                              & "call CreateFolders_83 -u=infodba -p=infodba -g=dba -f=Project" & FileName & ".txt" & Space(1) & "-sl=sl" & FileName & ".txt" & Space(1) & "-el=el" & FileName & ".txt" & _

                              Space(1) & "-dl=dl" & FileName & ".txt"

                   

                   

                              FSOFile.Close

                             

                              End If

                         End If

                  • Re: How to store user settiings for Solidworks custom addin/macro application?
                    Jim Sculley

                    See IGet3rdPartyStorage in the ModelDoc2 object.

                     

                    Jim S.

                    1 person found this helpful
                      • Re: How to store user settiings for Solidworks custom addin/macro application?
                        Cgtech Cgtech

                        Hello JIm,

                         

                        Thanks a lot for reply,

                         

                        And I am sorry for getting back very late.

                         

                        I looked into the documentation of the API and It seems this is exactly what I was looking for. But I want to create an XML document to save all settings,

                        is there a way you can provide me a sample code which uses IStream output object from IGet3rdPartyStorage API, to read or write XML file.

                         

                        Also, as per documentation (NOTE 1), how do I register the name of the storage stream.

                         

                        Thanks a lot again,

                        MD

                          • Re: How to store user settiings for Solidworks custom addin/macro application?
                            Jim Sculley

                            The 3rd party storage stream is simply a stream of bytes.  XML is nothing more than a text format that can easily be converted to bytes and stored in the SW file:

                             

                            IStream stream = mDoc.IGet3rdPartyStorage("com.somecompany.example.stream", true);
                            

                            string sampleXML =@"<?xml version=""1.0"" encoding=""UTF-8""?>

                                            <program name = ""SolidWorks"">

                                                <version>2014</version>

                                            </Program>";

                            byte[] bytes = Encoding.ASCII.GetBytes(sampleXML);

                            IntPtr pcbWritten = Marshal.AllocHGlobal(sizeof(int)); //Create a pointer in C#

                            stream.Write(bytes, bytes.Length, pcbWritten);

                            int bytesWritten = Marshal.ReadInt32(pcbWritten); //Get the value pointed to by the pointer

                            Marshal.FreeHGlobal(pcbWritten); //Release the memory allocated by the pointer

                            mDoc.IRelease3rdPartyStorage("com.somecompany.example.stream");

                             

                            To get the XML back out, you do something like this:

                            IStream stream = mDoc.IGet3rdPartyStorage("com.somecompany.example.stream", false);
                            int bufLength = 8192;
                            byte[] buffer = new byte[bufLength];
                            StringBuilder sb = new StringBuilder();
                            IntPtr pcbRead = Marshal.AllocHGlobal(sizeof(int));
                            bool finished = false;
                            while (!finished)
                            {
                                stream.Read(buffer, bufLength, pcbRead);
                                int bytesReturned = Marshal.ReadInt32(pcbRead);
                                for (int i = 0; i < bytesReturned; i++)
                                {
                                    sb.Append(Convert.ToChar(buffer[i]));
                                }
                                if (bytesReturned == 0)
                                {
                                    finished = true;
                                }
                            }
                            iSwApp.SendMsgToUser2(sb.ToString(), (int)swMessageBoxIcon_e.swMbInformation, (int)swMessageBoxBtn_e.swMbOk);  //this example just displays the XML in a MessageBox
                            Marshal.FreeHGlobal(pcbRead);
                            mDoc.IRelease3rdPartyStorage("com.somecompany.example.stream");
                            

                             

                            You will most likely want to load the stream in response to the LoadFromStorageNotify event and save the stream in response to the SaveToStorageNotify event.  When your add-in makes changes to the XML, you can mark the document as dirty using IModelDoc2::SetSaveFlag so that when the document is saved, the SaveToStorageNotify event is fired, giving you the opportunity to save the new XML data to the stream.

                             

                            Registering the name of the stream isn't required for the code to function.  You simply want to ensure that your name won't conflict with the names of any other streams (created by SW or other add-ins) in the file.  The easiest way to ensure that is to use a naming convention.  The Java programming language has a similar issue with package names.  There is a well defined, simple naming convention described here:  Naming a Package

                             

                            The 30 character limit that SW imposes may require some creativity on your part if your company name is long.

                             

                            Jim S.

                    • Re: How to store user settiings for Solidworks custom addin/macro application?
                      Chris Mackedanz

                      Reading and writing to a file is not very hard.

                       

                      There are a few really well written guides out there.

                       

                      The best one I found though basically used the file scripting object library to read and write.  Then he had very specific rules as to how the text file would be formatted.

                      He would put things into sections, each section would have a header enclosed in brackets, [] between every section would be a blank line.

                       

                      Example ->

                      [Section 1]

                      a = 1

                      b = 2

                       

                      [Section 2]

                      The quick brown fox

                      jumped over the log

                      and I don't remember the rest...

                       

                       

                      Then using something like ->

                       

                      Open Source For Input as #1

                           Do While Not EOF(1)

                               Input #1, Reader

                                If Reader = "[Section 1]" Then

                                     Do While Not EOF(1)

                                     input #1, LineItem

                                          If LineItem <> "" Then

                                               'Probably would put in code to split the string into 2 parts, using the = sign as the delimiter, before would be the variable you want to read, and after the value.

                                               'you would have to put them into two temporary variables and then use a select case to match it up with the variables in your macro.

                                          Else

                                               GoTo EndRead1

                                          End If

                                     Loop

                                End If

                            Loop

                      EndRead1:

                       

                      Close #1

                       

                       

                       

                      I usually write this up as a sub/function and then I can call it for each section in the file I want to read...

                       

                      i.e.

                       

                      ReadProperties FileToRead, SectionToRead

                       

                      To save the file out, you would use the FSO but you would open a file for write access and then just write out each variable and it's value on a line.  You could break it up so you could easily read the text file, so say all, listboxes are one group, all comboboxes are another, all text boxes are their own.

                       

                      Oh, one other thing you can give your file pretty much any file extension you want, I usually use .ini for a configuration file.

                      • Re: How to store user settiings for Solidworks custom addin/macro application?
                        Dan Miel

                        Another way to save settings for checkboxes, radio buttons, etc. in Visual Basic is to use Savesetting and Getsetting. Savesetting saves the information to "HKEY_CURRENT_USER>SOFTWARE > VB AND VBA Program Settings" in your computers registry.  Getsetting reads the information back.

                        Dan Miel