Hide Table of Contents

Add and Remove Files from Pack and Go (C#)

This example shows how to add SolidWorks, render reference, and non-SolidWorks files to Pack and Go. This example also shows how to remove a non-SolidWorks file from Pack and Go.

//-------------------------------------------
// Preconditions:
// 1. Specified assembly exists.
// 2. The folder, c:\temp, exists.
// 3. Open the Immediate window.
//
// Postconditions:
// 1. Names of SolidWorks, render reference, and
//    non-SolidWorks files for Pack and Go are
//    printed to the Immediate window.
// 2. Name of non-SolidWorks file to remove is
//    printed to Immediate window.
// 3. SolidWorks, render reference, and
//    non-SolidWorks files are packed up and
//    copied to c:\temp.
// 4. Examine c:\temp to verify.
//-------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace AddExternalDocumentsPackAndGo.csproj
{
    
partial class SolidWorksMacro
    {
        
public void Main()
        {
            ModelDoc2 swModel =
default(ModelDoc2);
            ModelDocExtension swModelDocExt =
default(ModelDocExtension);
            PackAndGo swPackAndGo =
default(PackAndGo);
            
string openFile = null;
            
int namesCount = 0;
            
int errors = 0;
            
int warnings = 0;
            
bool status = false;
            
int i = 0;
            
object[] renderReferences = null;
            
string myPath = null;
            
object statuses = null;

            
// Open assembly document
            openFile = "C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\EDraw\\claw\\claw-mechanism.sldasm";
            swModel = (ModelDoc2)swApp.OpenDoc6(openFile, (
int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            swModelDocExt = (ModelDocExtension)swModel.Extension;

            
// Get Pack and Go object
            Debug.Print("Pack and Go");
            swPackAndGo = (PackAndGo)swModelDocExt.GetPackAndGo();

            
// Get number of documents in assembly
            namesCount = swPackAndGo.GetDocumentNamesCount();

            
// Get current paths and filenames of the assembly's documents
            object fileNames;
            
object[] pgFileNames = new object[namesCount - 1];
            status = swPackAndGo.GetDocumentNames(
out fileNames);
            pgFileNames = (
object[])fileNames;

            
Debug.Print("");
            
Debug.Print("  Add SolidWorks files' paths and filenames: ");
            
if ((pgFileNames != null))
            {
                
for (i = 0; i <= pgFileNames.GetUpperBound(0); i++)
                {
                    
Debug.Print("    The path and filename is: " + pgFileNames[i]);
                }
            }

            
// Set document paths and names for Pack and Go
            status = swPackAndGo.SetDocumentSaveToNames(pgFileNames);

            
// Get the render stock references in this assembly
            // and print them to the Immediate window
            Debug.Print(" ");
            renderReferences = (
object[])swModelDocExt.GetRenderStockReferences();
            
Debug.Print("  Add render references:");
            
for (i = 0; i <= renderReferences.GetUpperBound(0); i++)
            {
                
Debug.Print("    The path and filename is: " + renderReferences[i]);
            }

            
// Add render stock files to Pack and Go
            status = swPackAndGo.AddExternalDocuments(renderReferences);

            
// Add other non-SolidWorks files to Pack and Go
            object[] otherFiles = new object[2];
            
string otherFile = "c:\\program files\\solidworks corp\\solidworks\\samples\\tutorial\\edraw\\claw\\claw-mechanism.easm";
            otherFiles[0] = (
object)otherFile;
            otherFile =
"c:\\program files\\solidworks corp\\solidworks\\samples\\tutorial\\edraw\\claw\\claw-mechanism.emodel_debugonly.xml";
            otherFiles[1] = (
object)otherFile;

            
Debug.Print(" ");
            
Debug.Print("  Add non-SolidWorks files:");
            
for (i = 0; i <= otherFiles.GetUpperBound(0); i++)
            {
                
Debug.Print("    The path and filename is: " + otherFiles[i]);
            }

            
// Add non-SolidWorks file to Pack and Go
            status = swPackAndGo.AddExternalDocuments(ObjectArrayToBStrWrapperArray(otherFiles));

            
// Remove one of the non-SolidWorks files from Pack and Go
            object[] delOtherFiles = new object[1];
            delOtherFiles[0] = (
object)otherFiles[0];

            
Debug.Print(" ");
            
Debug.Print("  Remove non-SolidWorks file:");
            
Debug.Print("    The path and filename is: " + delOtherFiles[0]);

            status = swPackAndGo.RemoveExternalDocuments(ObjectArrayToBStrWrapperArray(delOtherFiles));

            
// Override path where to save documents
            myPath = "c:\\temp\\";
            status = swPackAndGo.SetSaveToName(
true, myPath);

            
// Pack and Go both SolidWorks and non-SolidWorks files
            statuses = swModelDocExt.SavePackAndGo(swPackAndGo);
        }

        
public BStrWrapper[] ObjectArrayToBStrWrapperArray(object[] SwObjects)
        {
            
int arraySize;
            arraySize = SwObjects.GetUpperBound(0);
            
BStrWrapper[] dispwrap = new BStrWrapper[arraySize + 1];
            
int arrayIndex;

            
for (arrayIndex = 0; arrayIndex < arraySize + 1; arrayIndex++)
            {
                dispwrap[arrayIndex] =
new BStrWrapper((string)(SwObjects[arrayIndex]));
            }

            
return dispwrap;

        }

        
/// <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:   Add and Remove Files from Pack and Go (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) 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.