7 Replies Latest reply on Aug 9, 2013 5:47 PM by Louis Bouchard

    C# add-in and OptisWorks Studio

    Louis Bouchard

      Hi,

       

      I don't know if this is the best place to ask about OptisWorks' API, but Optis doesn't have any user forums that I know of.

       

      In order to do anything, I have to obtain an OptisWorksInstance.  Their user guide indicates the following VB code to achieve this:

       

      Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

      Set Instance = Catalog.Find(ModelDoc)

      Set PartPreferences=Instance.GetPartPreferences()

       

      ... which I have translated in C#:

       

      OptisWorksCatalog catalog = null;

      OptisWorksInstance instance = null;

      catalog = new OptisWorksCatalog();

      instance = (OptisWorksInstance) catalog.Find( swApp.ActiveDoc );



        • Re: C# add-in and OptisWorks Studio
          Louis Bouchard

          (oops, I inadvertantly pressed submit, I was not done.  Is it not possible to edit my original post?)

           

          the last line:

          instance = (OptisWorksInstance) catalog.Find( swApp.ActiveDoc );

           

          .. simply crashes Solidworks.  The variable catalog is not null, therefore it is not due to a null pointer exception.  I don't really know where to look for answers, google finds nothing.

           

          Has anyone written an Add-in using OptisWorks' API not in VB but in C#?  The OptisWorks user's guide only has examples in VB.

           

          thanks in advance,

           

          Louis Bouchard

            • Re: C# add-in and OptisWorks Studio
              Louis Bouchard

              I am zeroing in on the problem, kind of..

               

              I tried putting the offending line in a try/catch, and lo and behold, I caught an exception: "Specified cast is not valid".  To see which cast is causing the problem, i just did the following:

               

              try {

                   catalog.Find( swApp.ActiveDoc ); // <--- I don't even assign

                                                    //   the return value to anything

              } catch (Exception e) {

                   MessageBox.Show(e.Message);

              }

               

              .. and it still catches "Specified cast is not valid".  the Find(..) method takes an 'object' as parameter, so I tried casting that explicitely:

              try {

                   catalog.Find( (object)swApp.ActiveDoc );

              } catch (Exception e) {

                   MessageBox.Show(e.Message);

              }

               

              ..to no avail.  It still catches the same  "Specified cast is not valid" exception.

               

              I don't really know where to go from here.

               

              Any ideas?

               


                • Re: C# add-in and OptisWorks Studio
                  Artem Taturevych

                  Hi Louis,

                   

                  Why do you think the OptisWorksCatalog::Find expects the IModelDoc2 but not string or some OptisWorks model wrapper class. The problem here is inside this function it should be something like that:

                   

                  OptisWorksInstance Find(object model)

                  {

                                 X myModel = (X)model;

                  ...

                  }

                  where the X is some type which cannot be casted from the IModelDoc2.

                   

                  Do you have any documentation about this method? Is the VBA sample working? If so what is the type of ModelDoc variable.

                   

                  You can also try to use Activator.CreateInstance to instantiate the catalog using the progId rather than using of constructor. Probably you have several versions and you instantiate wrong one which is not compatible with this version of SolidWorks.

                  ____________________________________________________

                  Regards,

                  Artem Taturevych, Application Engineer at Intercad (Australia)

                   

                  translationXpert – add-in to translate SolidWorks models

                  myIntercad an integrated tool for SolidWorks Professionals

                  LinkedIn SolidWorks API Education Group

                    • Re: C# add-in and OptisWorks Studio
                      Louis Bouchard

                      Ok so I tried creating the catalog via Activator:

                       

                      Type t = Type.GetTypeFromProgID("OptisWorksCatalog.Catalog");

                      catalog = (OptisWorksCatalog)Activator.CreateInstance(t);

                       

                      the catalog is created just fine, but the error at .Find(...) remains.

                       

                       

                      I also tried, as you suggested, to run a VBA macro example, with similar results:

                       

                      Sub main()

                        Dim swApp As Object

                        Dim Catalog As OptisWorksCatalog

                        Dim Instance As OptisWorksInstance

                        Dim md As ModelDoc2

                       

                        Set swApp = GetObject(Class:="OptisWorksStudio.Application")

                        Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

                        Set md = swApp.ActiveDoc

                        Set Instance = Catalog.Find(md)  '<--- CRASHES HERE TOO!

                      End Sub

                       

                      Here, Catalog.Find(..) triggers runtime error 430 : Class does not support Automation or does not support expected interface.  According to msdn, it either means that, like the error in the C# project, there is indeed a mismatch with class types, OR that there is a conflict with DLLs somewhere.

                       

                      still investigating...

                        • Re: C# add-in and OptisWorks Studio
                          Jim Sculley

                          Please post the text of the OptisWorks API documentation for the Find() method.  Without that, it is unlikely that anyone here can help you.

                           

                          Jim S.

                            • Re: C# add-in and OptisWorks Studio
                              Louis Bouchard

                              sure, here it is:

                              ________________________________________

                              Find Method

                              Description

                              It returns an object OptisWorksInstance

                              Syntax

                              object.Find(pDispSearch As Object)

                              • object: OptisWorksCatalog object
                              • pDispSearch: SolidWorks ModelDoc2 object

                              Example

                               

                              Dim swApp As Object

                              Dim ModelDoc As ModelDoc2

                              Dim Catalog As OptisWorksCatalog

                              Dim Instance As OptisWorksInstance

                               

                              Sub main()

                                 Set swApp = Application.SldWorks

                                 Set ModelDoc = swApp.ActiveDoc

                                 Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

                                 Set Instance = Catalog.Find(ModelDoc)

                              End Sub

                              ________________________________________

                               

                               

                              as you can see, not much information here.

                            • Re: C# add-in and OptisWorks Studio
                              Louis Bouchard

                              I just tried running a VBA macro a colleague had previously made (and it worked on his machine).  Well, it doesn't work on mine, I put a few MsgBoxes and saw the same Class does not support Automation error triggered by the Find() method.

                               

                              So I guess the problem is with my installation.

                               

                              EDIT:

                              I tried my code on another machine and it worked.  I also noticed that OptisWorks on that machine has a 'optisworks' menu on the top toolbar, and it is not there on mine.  I assume something went terribly wrong with the initial installation (not that I remember seeing any errors).  So I am reinstalling it.  Fingers crossed...

                               

                              EDIT 2:

                              Reinstalled it, and the problem remains.  I have the week off, so I'll try fixing the installation when I get back.  But I think I can say that the initial question is resolved, it's just that my initial assumption was wrong.