6 Replies Latest reply on Jan 28, 2013 9:53 PM by Artem Taturevych

    ROV design macro

    Chris Szlatenyi

      Hi all, I've been working on a macro to aid high school kids with the design of a remotely operated underwater vehicle. I figure I will share here if anyone is interested and maybe get some feedback. Also included is a little test assembly I've been using.

       

      First off, a warning: I have zero programming experience and this is as much an exercise in learning c#, solidworks api as it is to create something useful, so my apologies for what I'm sure is sloppy inefficient code.

       

       

      What it does:

      --Inserts a new 3DSketch "CG" with a point at the center of gravity

      --Calculates Center of Volume and inserts a new 3DSketch "CB" with a point at the center of buoyancy.

      --Returns:

      ----Mass, Center of Mass

      ----Volume, Center of Buoyancy

      ----Air Weight, Water Weight, % Buoyancy

      ----Stable Pitch (about z) and Roll (about x) Angles

       

      Notes:

      File must be an assembly

      It excludes hidden and suppressed components

      Uses system units (MKS)

       

      The center of buoyancy calc is probably the most useful thing here. I can upload a macro that is just that if anyone is interested.

       

      In the future, I'd like to transition this to an add-in application and maybe somehow incorporate some motion simulation with thrusters. Thanks for taking a look!

       

      Chris

        • Re: ROV design macro
          Keith Rice

          Hi Chris,

           

          Welcome to the forums. I think its pretty cool that you are using not just SolidWorks but the API with high school students. Great idea for a project as well.

           

          Glancing over your code, I can tell you why you aren't getting Debug.Print to print to the output or immediate window (which I assume is what you are trying to do). Along with your other using statements at the top of your code, you need this:

           

          using System.Diagnostics;

           

          Then you should get output to either the Output or Immediate window. Which window receives output is determined by whether the following check box is checked:

          Tools-->Options-->Debugging-->General-->Redirect all Output Window text to the Immediate Window.

           

          To open either of these windows, go to Debug-->Windows.

           

          On a slightly different topic, I should note that if you want to share your macro project (with us or whoever else) then you should just zip up the entire folder rather than just sending the .cs file. That contains the code but it is not the project, so it creates some extra work for whoever is on the receiving end.

           

          Finally, I am curious, since you are new to the SolidWorks API and programming, why are you using C#? This is one of the harder languages to use with the API (unless you have a C++ or Java background, I suppose). VBA is quicker to learn and generally faster to write macros with, plus there is vastly more documentation and tutorials available in VBA than in C# or even VB.NET. I cover this topic and more beginner concerns in this blog post, which you may enjoy reading: 7 Mistakes New SolidWorks API Programmers Make  Of course, if you are certain you want to learn the API with C#, more power to you!

           

          So there's my feedback. If you have a more specific question about the API, such why you are (or are not) getting a particular result, then I would recommend creating a code sample that demonstrates that issue alone. In other words, isolate the problem from the rest of your code. This will make the issue vastly easier to debug---for you and for those you ask for help.

           

          On the topic of forum use: additional, separate questions (e.g., "why am I not getting X result from this line of code?") ought to be posted as new threads, rather than trying to ask all of your questions in one thread. This reduces a lot of clutter and confusion and will increase the likelihood of someone helping you. When someone does answer your question, be sure to mark their post as the correct answer. This lets others know that the question is answered and rewards that person for helping you.

           

          Keith

          Video Tutorials for the SolidWorks API

            • Re: ROV design macro
              Chris Szlatenyi

              Thanks for your thoughts Keith!

               

              Yes, I never could get debug to work and still can't :/ When I try and debug from VSTA I get: Untitled.jpg

              I tried for a while to solve the issue and then gave up.

               

              Also, I went with C# as C++ is something I would like to learn in the future... in hindsight VBA might have been a better choice, but I'm happy enough using C#. Wish I had seen your 7 mistakes article earlier!

               

              Chris

                • Re: ROV design macro
                  Keith Rice

                  This may be a problem with your references. Try creating a brand new VSTA macro and copying your code into the new macro. Then see if you can debug.

                   

                  Next, try recording a C# macro. Can you debug it? What about a VB.NET macro? Next, try debugging the attached macro that I created on my computer.

                   

                  If you are unable to debug any VSTA macro then VSTA may need to be reinstalled.

                   

                  In your original post I missed the part where you said "In the future, I'd like to transition this to an add-in application". In that case, you made the right choice of going with a .NET language like C# since VBA can't be used to create addins.

                   

                  Keith

                  Video Tutorials for the SolidWorks API

              • Re: ROV design macro
                Artem Taturevych

                Hi Chris,

                 

                What exactly the problem with the debugger do you have? As I understand you can build the project but cannot debug it, is that correct?

                 

                I have done the following steps:

                 

                1) Create new VSTA c# macro

                2) Replace the SolidWorksMacro.cs with your file

                3) Go to Tools->Macro->Edit and selected the macro project

                4) Now use breakpoints and F10/F11 to debug the macro. And it is debugging nicely

                 

                P.S. If you say that this is one of your first projects in c# and this is learning I would say it is quite impressive. The functions are logically split and you are using helper attributes like regions, comments as well as .NET Framework standard classes : lists, math, regex. So you are on the right direction.

                 

                Let me share my though about the choosing of programming language. I agree that the VBA/VB.NET are one of the most user-friendly languages which may be a sense to begin with. But as I see from your code you already got basic concepts of programming. Later writing bigger projects  you will no longer need these 'sweet things' while you need some more convenient to use as well as more elegant and compact. Personally I prefer a c# rather than VB.NET. VBA is something on separate note which you should use to troubleshoot the problems or something like that but it cannot be considered as the language to write the medium or big projects. C++ is still the best choice if you need to have better performance or use some low-level code. At some points it may be a solution to mix your project with c# and c++.

                 

                Good luck!

                __________________________

                Regards,

                Artem Taturevych

                Application Engineer at Intercad

                http://intercad.com.au/

                Tel: +61 2 9454 4444

                  • Re: ROV design macro
                    Chris Szlatenyi

                    Hi, thanks for the input Artem. Correct, I can build but not debug any macro... I don't think it is an issue with the code itself but rather something with VSTA or my computer. I reinstalled solidwokrs including the API SDK which did not help the issue. Is there another way to just reinstall VSTA?

                     

                    Also, I have Visual Studio 2010 installed which I would rather use. Do you have any guidance on converting a macro to a Visual Studio project? Attached is the conversion report when trying to open the macro solution in Visual Studio. I also tried copying files and adding references manually, but I was probably missing something.

                     

                    Thanks,

                    Chris

                      • Re: ROV design macro
                        Artem Taturevych

                        Hi Chris,

                         

                        Yes, it is possible to reset the VSTA. Please check this topic: https://forum.solidworks.com/message/160642#160642

                         

                        If your final target to just develop the tool and there is no any precondition to have a macro I would suggest you to create either stand-alone application or add-in. For debugging purposes stand-alone is more preferable because this is out-of-process application and you can navigate SolidWorks while debugging.

                         

                        Fortunately it is very easy to switch from macro to stand-alone. There are only two extra steps you should do:

                         

                        1) Add references to SolidWork libraries

                        2) Initially connect to SolidWorks application.

                         

                        I have moved your code to stand-alone console application. Please check the attachment (I'm using VS 2008 so please upgrade the project to VS 2010).

                         

                        When you open the solution in your VS you will find the Program.cs/Main - entry point and some initialization. Then I just added your .cs macro and use it. It is better to move the functions to the module directly but anyways I just shown how you can use and debug from stand-alone application.

                         

                        Please let me know if you have any questions.

                        __________________________

                        Regards,

                        Artem Taturevych

                        Application Engineer at Intercad

                        http://intercad.com.au/

                        Tel: +61 2 9454 4444