Example #1
0
        public static void SaveAsIges(this IBody2 body, string igesFile, bool hidden = false)
        {
            SldWorks app = SwAddinBase.Active.SwApp;

            var doc = app.CreateHiddenDocument(hidden: hidden);

            doc.Extension.SetUserPreferenceInteger
                ((int)(swUserPreferenceIntegerValue_e.swUnitSystem)
                , 0
                , ((int)(swUnitSystem_e.swUnitSystem_MKS)));

            try
            {
                var partDoc = (PartDoc)doc;

                partDoc.CreateFeatureFromBody3(body, false, 0);

                int errorsi   = 0;
                int warningsi = 0;

                app.ActivateDoc3
                    (doc.GetTitle(), false, (int)swRebuildOnActivation_e.swRebuildActiveDoc, ref errorsi);


                // http://help.solidworks.com/2013/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~SaveAs.html
                // Note the doc says:
                // To save as an IGES, STL, or STEP file, the document to convert must be the active document. Before calling this method:
                // Call ISldWorks::ActivateDoc3 to make the document to convert the active document.
                // Call ISldWorks::ActiveDoc to get the active document.

                var status = doc // Note that this does not return the correct doc if the doc is hidden
                             .Extension
                             .SaveAs
                                 (igesFile
                                 , (int)swSaveAsVersion_e.swSaveAsCurrentVersion
                                 , (int)swSaveAsOptions_e.swSaveAsOptions_Silent
                                 , null
                                 , ref errorsi
                                 , ref warningsi
                                 );

                if (!status)
                {
                    throw new Exception($"Failed to save {igesFile}. Got error bitmask {errorsi}");
                }
            }
            finally
            {
                app.QuitDoc(doc.GetTitle());
            }
        }