1 Reply Latest reply on Jul 25, 2017 7:49 AM by Amen Allah Jlili

    API Tutorial (C#) doesn't work with SW2017?

    Mark Nelson

      I was having trouble following the C# version of the API tutorial where you make a form that prompts for the radius and depth of a new hole added to a candlestick holder. I asked an accomplished C# programmer at work assist me (let's just call him "Oleg" for fun), he suspects the tutorial is broken and suggests I get help from the SolidWorks side. This is my first attempt at learning API, so I don't know if the problem existed or not for earlier SW versions.


      I was able to debug one problem myself. In "Recording a Macro", steps 4 and 5 should be reversed. If you start recording the macro before selecting the top face, then the macro will always add the hole to that top face. The intent seems to be adding that special hole to any face you select before activating the macro, so the top face selection can't be part of the recording or the hole will always go on that top face no matter what face you select first. Also at "Adding Variables for Controls", step 3, it says to insert some code "between swDoc = ((ModelDoc2)(swApp.ActiveDoc)); and //COMMENT OUT THIS LINE OF CODE", but I can't find that comment line in the project code produced by recording the macro in SW2017. Oleg says all the declarations and members look good, so I just insert the snippet immediate afterswDoc = ((ModelDoc2)(swApp.ActiveDoc)); . Movin' on.


      Big trouble seems to begin at the "Saving the Code for the Form" step 1. I'm instructed to locatedz = darray[2];, and replace everything after that line with the snippet provided. However,dz = darray[2]; never appears in the code, in fact the debug never completes because neither dx, dy nor dz are ever declared, and build errors prevent further progress, since these names dx, dy, dz "never appear in the current context." We tried a work-around by just replacing everything from the SketchSegment swSketchSegment = null; line on down with the snippet, but needed to delete one of the extra end curly brackets from the snippet to make it "ctrl-k-d" nicely. It was one of the two between the "swFeature =" line and "//KEEP THESE LINES OF CODE"


      My ultimate goal is a script that outputs a table of interference volumes across ranges of two input variable dimensions for one of the two components in an assembly. A form will be nice to set min max and increment for those inputs, would like to get it working. Maybe the full source code from the tutorial would be good, instead of piecing it together with snippets? Thanks for any help or guidance on using C# API in SW2017. -Mark