Hide 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:
// 1. Verify that the specified assembly file exists.
// 2. Verify that c:\temp exists.
// 3. Open the Immediate window.
//
// Postconditions:
// 1. Saves the specified assembly file and its
//    component files as PDF files to the c:\temp.
// 2. Examine the Immediate window and c:\temp.
//--------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
using System.IO;

namespace Macro1CSharp.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:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2018\\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;
            }
            else 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;
    }
}



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:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

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) 2018 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.