5 Replies Latest reply on Mar 4, 2015 12:25 AM by Charlie Fan

    Cannot get "OpenDoc6" to work in C#, am I doing something wrong?

    Charlie Fan

      Just cannot make the following to work, it returns a few error:

       

      long errors, warnings;

      swModel = swApp.OpenDoc6(fileName, SwConst.swDocumentTypes_e.swDocASSEMBLY, SwConst.swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings);

       

      Can someone please help thanks!!!

        • Re: Cannot get "OpenDoc6" to work in C#, am I doing something wrong?
          Keith Rice

          The following will open an assembly in C#. Open a new C# macro and replace the original code with this:

           

          using SolidWorks.Interop.sldworks;
          using SolidWorks.Interop.swconst;
          using System.Runtime.InteropServices;
          using System;
          
          namespace Macro1.csproj
          {
              public partial class SolidWorksMacro
              {
          
                  public void Main()
                  {
                      string fileName = @"C:\test.SLDASM";
                      int err = 0;
                      int warn = 0;
                      ModelDoc2 swModel = (ModelDoc2)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocASSEMBLY, 
                          (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref err, ref warn);
                  }
          
                  public SldWorks swApp;
              }
          }
          

           

          You have many problems with your arguments --- not explicitly converting data types, using unrecognized libraries (SwConst), and not using the "ref" keyword to pass the last two arguments by reference rather than by value.

           

          Not sure of your goals, but if you must use C#, I would strongly recommend you spend time learning the language by itself before you try to use with the SolidWorks API. There's so many idiosyncrasies with both the language itself and its usage with the SolidWorks API.

           

          Keith

          SolidWorks API Tutorials

            • Re: Cannot get "OpenDoc6" to work in C#, am I doing something wrong?
              Charlie Fan

              Thanks, still a bit different when I got it working though... but mostly correct.

              • Re: Cannot get "OpenDoc6" to work in C#, am I doing something wrong?
                Charlie Fan

                BTW, what is a C# macro?

                 

                I can't find it in my VS2013.

                 

                I used the command line C# in VS2013, and that's why I have to change the code you provided above to get it finally working.

                  • Re: Cannot get "OpenDoc6" to work in C#, am I doing something wrong?
                    Keith Rice

                    Charlie,

                     

                    Thanks, still a bit different when I got it working though... but mostly correct. ... I used the command line C# in VS2013, and that's why I have to change the code you provided above to get it finally working.

                     

                    I am guessing that the discrepancy you are referring to is regarding the "swconst". What libraries are you referencing? You should be referencing SolidWorks.Interop.swconst, not swconst.tlb, which is probably causing a discrepancy between our code. The language works the same regardless of whether one is creating a macro, add-in, or stand-alone.

                     

                    BTW, what is a C# macro? I can't find it in my VS2013.

                    It is a macro written in C# using the Visual Studio Tools for Applications environment. To create a C# macro:

                     

                    1. Open SolidWorks

                    2. Tools-->Macro-->New

                    3. Select "SW VSTA C# Macro" in the Save As Type dropdown.

                    4. Click save and after a few seconds (sometimes it takes 10 or more) the VSTA environment will load.

                     

                    I would recommend using VSTA macros as sandbox for learning the API in a particular language before trying to create an add-in or stand-alone.

                     

                    Keith

                    SolidWorks API Training

                      • Re: Cannot get "OpenDoc6" to work in C#, am I doing something wrong?
                        Charlie Fan

                        Hi Thanks.

                         

                        Very useful. I reinstalled my solidworks to make VSTA work on my machine. It is not installed by default on Windows 8.1

                         

                        The code difference is mainly about some variables need "static" on it, and/or declared outside the function but within class. Tricky things will happen since the main function should be static in console app of C#, this will change the declaration of variable as well. I will look into that as well to see how to suit it better when developing stand-alone apps.

                         

                        I am trying to get solidworks running on web server and use asp.net to drive it. Have to use C# because vb is not as popular as C# on asp.net.