Exporting to .STL doesn't use the selected resolution

Hi

I'm using SW 2011 x64 SP5 and using the API in C# to generate a part, saving the part and exporting it to .STL.

I can get the code to do all these things, but there is a problem with the exported .STL file, which is generated using a different resultion from what I specified.

A simplified code to reprocude the problem is included below.

Steps to reproduce:

1) The code generates a part and saves a .SLDPRT file and a .STL files as it should. On my system, the generated .STL file is 63084 bytes.

2) Open the .SLDPRT file manually and export to a .STL file without changing any settings. This file is still 63084 bytes.

3) Same as 2), but with the addition of opening the export settings dialog, pressing ok and saving the file. This file is now 35484 bytes.

I want the code to output the file in step 3. It seems to me, that the settings that I specify are not used and some standard values are used instead, because when I change the resolution of the .STL file, the file size in step 1 and 2 does not change, but the size in step 3 changes.

Please, help.

The code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.IO;

namespace SW_Problem_Reproduce
{
    public class SWProblem
    {
        protected SldWorks swApp = null;
        protected ModelDoc2 swDoc = null;
        protected PartDoc swPart = null;
        protected bool boolstatus = false;
        protected int longstatus = 0;

        public void RecreateProblem()
        {
            double STL_Angle_Tolerance = 20.0; // <--------- This is the value that I cannot get SW to use.
            string partName = "STL_Problem";

            //Change these values to fit your system
            string savePath = @"C:\SW_Parts\";

            if (false == Directory.Exists(savePath)) Directory.CreateDirectory(savePath);
            string saveFileWithoutExtension = Path.Combine(savePath, partName);

            // Start SolidWorks
            swApp = new SldWorks();
            swApp.Visible = true;

            // Generate the part
            swDoc = (ModelDoc2)swApp.NewPart();
            swApp.ActivateDoc2(partName, false, ref longstatus);
            swDoc = ((ModelDoc2)(swApp.ActiveDoc));
            ModelView myModelView = null;
            myModelView = ((ModelView)(swDoc.ActiveView));
            myModelView.FrameState = ((int)(swWindowState_e.swWindowMaximized));
            boolstatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
            swDoc.SketchManager.InsertSketch(true);
            swDoc.ClearSelection2(true);
            SketchSegment skSegment = null;
            skSegment = ((SketchSegment)(swDoc.SketchManager.CreateCircle(0, 0, 0, 20.0e-3, 0, 0)));
            swDoc.ClearSelection2(true);
            swDoc.SketchManager.InsertSketch(true);
            swDoc.ShowNamedView2("*Trimetric", 8);
            swDoc.ClearSelection2(true);
            boolstatus = swDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, false, 0, null, 0);
            Feature myFeature = null;
            myFeature = ((Feature)(swDoc.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, 30.0e-3, 1, false, false, false, false, 1745329251994, 1745329251994, false, false, false, false, true, true, true, 0, 0, false)));
            swDoc.ISelectionManager.EnableContourSelection = false;

            // Set the STL export parameters. Both on swApp and swDoc to be sure.
            boolstatus = swApp.SetUserPreferenceDoubleValue(((int)(swUserPreferenceDoubleValue_e.swSTLDeviation)), 0.001e-3); //5e-6  // Given in m
            boolstatus = swApp.SetUserPreferenceDoubleValue(((int)(swUserPreferenceDoubleValue_e.swSTLAngleTolerance)), STL_Angle_Tolerance * Math.PI / 180.0); // Given in rad
            boolstatus = swApp.SetUserPreferenceIntegerValue(((int)(swUserPreferenceIntegerValue_e.swExportStlUnits)), ((int)(swLengthUnit_e.swMETER)));
            swApp.SetUserPreferenceToggle((int)(swUserPreferenceToggle_e.swSTLShowInfoOnSave), false);
            swApp.SetUserPreferenceToggle((int)(swUserPreferenceToggle_e.swSTLComponentsIntoOneFile), true);

            boolstatus = swDoc.SetUserPreferenceDoubleValue(((int)(swUserPreferenceDoubleValue_e.swSTLDeviation)), 0.001e-3); //5e-6  // Given in m
            boolstatus = swDoc.SetUserPreferenceDoubleValue(((int)(swUserPreferenceDoubleValue_e.swSTLAngleTolerance)), STL_Angle_Tolerance * Math.PI / 180.0); // Given in rad
            boolstatus = swDoc.SetUserPreferenceIntegerValue(((int)(swUserPreferenceIntegerValue_e.swExportStlUnits)), ((int)(swLengthUnit_e.swMETER)));
            swDoc.SetUserPreferenceToggle((int)(swUserPreferenceToggle_e.swSTLShowInfoOnSave), false);
            swDoc.SetUserPreferenceToggle((int)(swUserPreferenceToggle_e.swSTLComponentsIntoOneFile), true);

            // Save the part first           
            int errors = 0;
            int warnings = 0;
            swDoc.Extension.SaveAs(saveFileWithoutExtension + ".SLDPRT", 0, 0, null, errors, warnings);

            // Then save the .STL
            swApp.ActivateDoc2(partName, false, ref errors);
            boolstatus = ((ModelDoc2)swApp.ActiveDoc).Extension.SaveAs(saveFileWithoutExtension + ".STL", 0, (int)(swSaveAsOptions_e.swSaveAsOptions_Copy), null, ref errors, ref warnings);

            // Close the document
            swApp.CloseDoc(partName);
            swDoc = null;
        }
    }
}

SolidworksApi macros