> Keep SolidWorks Invisible While Activating Documents Example (C#)
Welcome
Getting Started
SolidWorks API Help
FeatureWorks API Help
SolidWorks Document Manager API Help
eDrawings API Help
SolidWorks Routing API Help
SolidWorks Simulation API Help
SolidWorks Utilities API Help
SolidWorks Workgroup PDM API Help
Hide Table of Contents Show Table of Contents

Keep SolidWorks Invisible While Activating Documents Example (C#)

This example shows how to keep SolidWorks invisible while activating SolidWorks documents, including assembly component files, and saving those documents as PDF files.

//--------------------------------------------------------------------------

// Preconditions: Specified file exists.

//

// Postconditions: The specified assembly file and its

// its components files are saved as PDF

// files to the c:\temp folder.

//------------------------------

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using System;

using System.Diagnostics;

using System.IO;

namespace KeepInvisibleFrameCSharp.csproj

{

    public partial class SolidWorksMacro

    {

        public ModelDoc2 swModel;

        public ModelDocExtension swExtension;

 

        public void Main()

        {

            Frame pFrame;

            string Document = null;

            string Output = null;

            try

            {

                // Allow SolidWorks to run in the background

                // and be invisible

                swApp.UserControl = false;

 

                // If the following property is true, then the

                // SolidWorks frame will be visible on a call to

                // ISldWorks::ActivateDoc2; so, set it to false

                swApp.Visible = false;

 

                // Keep SolidWorks frame invisible when

                // ISldWorks::ActivateDoc2 is called

                pFrame = (Frame)swApp.Frame();

                pFrame.KeepInvisible = true;

 

                Document = "c:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\advdrawings\\blade shaft.sldasm";

                Output = "c:\\temp\\";

                Debug.Print("--- Save files as PDF ---");

                SaveToPDF(Document, Output);

                swApp.CloseAllDocuments(true);

                Debug.Print("--- Done ---");

 

                // Show SolidWorks frame and SolidWorks

                pFrame.KeepInvisible = false;

                swApp.Visible = true;

            }

            catch

            {

                Debug.Print("Execution failed.");

            }

        }

 

        private void SaveToPDF(string docFileName, string outputpath)

        {

            AssemblyDoc swAssembly = default(AssemblyDoc);

            int doctype = 0;

            int errors = 0;

            int warnings = 0;

            string modelpath = "";

            string modelFileName = "";

            string convFileName = "";

            bool Success = false;

            object[] vComponents = null;

            long i = 0;

 

            // Determine the type of SolidWorks file based on

            // its filename extension

            string extension = "";

            extension = Path.GetExtension(docFileName);

            if (extension == ".sldprt")

            {

                doctype = (int)swDocumentTypes_e.swDocPART;

            }

            if (extension == ".SLDPRT")

            {

                doctype = (int)swDocumentTypes_e.swDocPART;

            }

            else if (extension == ".sldasm")

            {

                doctype = (int)swDocumentTypes_e.swDocASSEMBLY;

            }

            else if (extension == ".SDLASM")

            {

                doctype = (int)swDocumentTypes_e.swDocASSEMBLY;

            }

            else if (extension == ".slddrw")

            {

                doctype = (int)swDocumentTypes_e.swDocDRAWING;

            }

            else if (extension == ".SLDDRW")

            {

                doctype = (int)swDocumentTypes_e.swDocDRAWING;

            }

            else

            {

                doctype = (int)swDocumentTypes_e.swDocNONE;

            }

 

            //Open document

            swModel = (ModelDoc2)swApp.OpenDoc6(docFileName, doctype, (int)swOpenDocOptions_e.swOpenDocOptions_Silent | (int)swOpenDocOptions_e.swOpenDocOptions_ReadOnly, "", ref errors, ref warnings);

            if (swModel == null)

            {

                Debug.Print("Failed to open document " + modelpath + ". Errors: " + errors);

            }

            // Activate the document, which should remain invisible

            // due to earlier call to IFrame::KeepInvisible

            swModel = (ModelDoc2)swApp.ActivateDoc2(docFileName, true, ref errors);

 

            // Build destination filename

            modelpath = swModel.GetPathName();

            modelFileName = Path.GetFileNameWithoutExtension(modelpath);

            convFileName = outputpath + modelFileName + ".pdf";

            swExtension = (ModelDocExtension)swModel.Extension;

 

            // Save document as PDF

            Success = swExtension.SaveAs(convFileName, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref errors, ref warnings);

            if (Success)

            {

                Debug.Print("Document, " + modelpath + ", saved as " + convFileName + ". ");

            }

            else

            {

                Debug.Print("Document not saved: ");

                Debug.Print(" Errors: " + errors + modelpath + " as " + convFileName + ". ");

            }

            // Process all components

            if (doctype == (int)swDocumentTypes_e.swDocASSEMBLY)

            {

                swAssembly = (AssemblyDoc)swModel;

                vComponents = (object[])swAssembly.GetComponents(true);

                for (i = 0; i < vComponents.Length; i++)

                {

                    Component2 swComponent = default(Component2);

                    swComponent = (Component2)vComponents[i];

                    SaveToPDF(swComponent.GetPathName(), outputpath);

                }

            }

        }

 

        /// <summary>

        /// The SldWorks swApp variable is pre-assigned for you.

        /// </summary>

        public SldWorks swApp;

    }

}



Related SolidWorks Forum Content

Provide feedback on this topic

SolidWorks welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Keep SolidWorks Invisible While Activating Documents Example (C#)
*Comment:  
x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2012 SP05

The search functionality within the web help is in a beta test phase and you may experience periodic delays or interruptions in its performance. These are the normal and ordinary features of a beta test and shall not under any circumstances give rise to any liability on the part of DS SolidWorks or its licensors. The topics within the Web-based help are not beta topics; they document API Help (English only) 2012 SP05.

To disable Web help from within SolidWorks and use local help instead, click Help > Use SolidWorks Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.