Hide Table of Contents

Pack and Go an Assembly (C#)

This example shows how to get the names of the path and files of an assembly document, assign them new names, add a prefix and suffix to the new names, and save the newly named files to a different path using the Pack and Go interface.

//------------------------------------------
// Preconditions:
// 1. Specified assembly exists.
// 2. The folder, c:\temp, exists.
// 3. Open the Immediate window.
// 4. Run the macro.
//
// Postconditions:
// 1. Names of the current path and filenames
//    of the assembly documents are printed to Immediate window.
// 2. Names of the default path and filenames to which to
//    save assembly documents are printed to Immediate window.
// 3. User-specified path and user-named filenames for assembly documents
//    created.
// 4. Prefix and suffix added to user-named filenames.
// 5. Names of user-specified path and user-named filenames printed to
//    Immediate window.
// 6. User-named files created in user-specified path using Pack and Go.
// 7. Examine c:\temp to verify.
//-------------------------------------------

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace PackAndGoCSharp.csproj
{

    
partial class SolidWorksMacro
    {


        
public void Main()
        {
            ModelDoc2 swModelDoc =
default(ModelDoc2);
            ModelDocExtension swModelDocExt =
default(ModelDocExtension);
            PackAndGo swPackAndGo =
default(PackAndGo);
            
string openFile = null;
            
string myFileName = null;
            
bool status = false;
            
int warnings = 0;
            
int errors = 0;
            
int i = 0;
            
int j = 0;
            
int namesCount = 0;
            
string myPath = null;
            
int[] statuses = null;

            
// Open assembly
            openFile = "C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\advdrawings\\handle.sldasm";
            swModelDoc = (ModelDoc2)swApp.OpenDoc6(openFile, (
int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            swModelDocExt = (ModelDocExtension)swModelDoc.Extension;

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

            
// Get number of documents in assembly
            namesCount = swPackAndGo.GetDocumentNamesCount();
            
Debug.Print("  Number of model documents: " + namesCount);
 

            // Include any drawings and simulation results
            swPackAndGo.IncludeDrawings =
true;
            Debug.Print(" Include drawings: " + swPackAndGo.IncludeDrawings);
            swPackAndGo.IncludeSimulationResults =
true;
            Debug.Print(" Include simulation results: " + swPackAndGo.IncludeSimulationResults);

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

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

            
// Get current save-to paths and filenames of the assembly's documents
            object pgFileStatus;
            status = swPackAndGo.GetDocumentSaveToNames(
out fileNames, out pgFileStatus);
            pgFileNames = (
string[])fileNames;
            
Debug.Print("");
            
Debug.Print("  Current default save-to filenames: ");
            
if ((pgFileNames != null))
            {
                
for (i = 0; i <= pgFileNames.GetUpperBound(0); i++)
                {
                    
Debug.Print("   The path and filename is: " + pgFileNames[i]);
                }
            }

            
// Folder where to save the files
            myPath = "C:\\temp\\";

            
// Create your own filenames for the model's documents
            string[] newFileNames = new string[namesCount];
            
Debug.Print("");
            
Debug.Print("  My Pack and Go path and filenames before adding prefix and suffix: ");
            j = 0;
            
for (i = 0; i <= namesCount - 1; i++)
            {
                myFileName = (
string)pgFileNames[i];
                
// Determine type of SolidWorks file based on file extension
                if (myFileName.EndsWith("sldprt"))
                {
                    myFileName = j +
".sldprt";
                }
                
else if (myFileName.EndsWith("sldasm"))
                {
                    myFileName = j +
".sldasm";
                }
                
else if (myFileName.EndsWith("slddrw"))
                {
                    myFileName = j +
".slddrw";
                }
                
else
                {
                    
// Only packing up SolidWorks files
                    return;
                }

                myFileName = myPath + myFileName;
                newFileNames[i] = (
string)myFileName;
                
Debug.Print("    My path and filename is: " + newFileNames[i]);
                j = j + 1;
            }

            // If a drawing document existed for the assembly or part document
            // used in this example, then you have to ensure that the
            // drawing document copied by Pack and Go references the assembly
            // or part document copied by Pack and Go and not the original
            // assembly or part document
            // Calling IPackAndGo::SetSaveToName sets the target for drawings
            // included in Pack and Go and overrides a call to
            // IPackAndGo::SetDocumentSaveToNames
            //status = swPackAndGo.SetSaveToName(true, myPath);


            
// Set document paths and filenames for Pack and Go
            BStrWrapper[] pgSetFileNames;
            pgSetFileNames = ObjectArrayToBStrWrapperArray(newFileNames);
            status = swPackAndGo.SetDocumentSaveToNames(pgSetFileNames);

            
// Add a prefix and suffix to the filenames
            swPackAndGo.AddPrefix = "SW";
            swPackAndGo.AddSuffix =
"PackAndGo";

            
// Verify document paths and filenames after adding prefix and suffix
            object getFileNames;
            
object getDocumentStatus;
            
string[] pgGetFileNames = new string[namesCount - 1];

            status = swPackAndGo.GetDocumentSaveToNames(
out getFileNames, out getDocumentStatus);
            pgGetFileNames = (
string[])getFileNames;
            
Debug.Print("");
            
Debug.Print("  My Pack and Go path and filenames after adding prefix and suffix: ");
            
for (i = 0; i <= namesCount - 1; i++)
            {
                
Debug.Print("    My path and filename is: " + pgGetFileNames[i]);
            }


            
// Pack and Go
            statuses = (int[])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:   Pack and Go an Assembly (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.