Hide Table of Contents

Save Solid Body to File Example (C#)

This example shows how to save a weldment member to another part document.

//----------------------------------------------------------------------------
// Preconditions: Open public_documents\samples\tutorial\api\weldment_box3.sldprt.
//
// Postconditions:
// 1. Updates the cut list in the weldment part.
// 2. Saves the first weldment member in the FeatureManager design tree to
//    RefWeldment1.sldprt, and saves the cut list properties in the parent part
//    to the cut list of the new part.
// 3. Opens RefWeldment1.sldprt and updates its cut list. (This step could
//    take a few minutes to complete.)
// 4. At
System.Diagnostics.Debugger.Break(), press F5.
// 5. Closes RefWeldment1.sldprt.
// 6. To verify steps 2 and 3, open and inspect RefWeldment1.sldprt, which resides
//    in the same folder as this macro.
//
// NOTE: Because weldment_box3.sldprt is used elsewhere, do not save changes.
//---------------------------------------------------------------------------

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
namespace CreatePartForSolidBody_CSharp.csproj
{
    
partial class SolidWorksMacro
    {

        
Feature swFeat;
        
BodyFolder swBodyFolder;
        
bool updateBoolstatus;
        
bool boolstatus;
        
int longstatus;
        
int longWarnings;
        
ModelDoc2 currentModel;
        
ModelDoc2 swModel;
        
int modelType;
        
string modelTitle;
        
        
public void Main()
        {
            currentModel = (
ModelDoc2)swApp.ActiveDoc;

            modelTitle = currentModel.GetTitle();
            modelType = currentModel.GetType();

            swFeat = (
Feature)currentModel.FirstFeature();
            
if (swFeat == null)
                ErrorMsg(swApp,
"Failed to get first feature");

            
while ((swFeat != null))
            {
                
if (swFeat.GetTypeName2() == "SolidBodyFolder")
                {
                    swBodyFolder = (
BodyFolder)swFeat.GetSpecificFeature2();
                    
if (swBodyFolder == null)
                        ErrorMsg(swApp,
"Failed to get body folder");

                    boolstatus = swBodyFolder.SetAutomaticCutList(
true);

                    boolstatus = swBodyFolder.UpdateCutList();

                    
break;
                }
                swFeat = (
Feature)swFeat.GetNextFeature();
            }

            updateBoolstatus =
false;

            swFeat = (
Feature)currentModel.FirstFeature();
            
if (swFeat == null)
                ErrorMsg(swApp,
"Failed to get first feature");

            
while ((swFeat != null))
            {
                
if (swFeat.GetTypeName2() == "WeldMemberFeat")
                {
                    boolstatus = swFeat.Select2(
false, 0);
                    
if (boolstatus == false)
                        ErrorMsg(swApp,
"Failed to select feature");

                    
// Save the selected solid body weldment member to another part,
                    // transferring the solid body's cut list properties to the new part's cut list;
                    // automatically creates a weldment and cut list folder
                    boolstatus = ((PartDoc)currentModel).SaveToFile3(swApp.GetCurrentMacroPathFolder() + "\\RefWeldment1" + ".sldprt", 1, swCutListTransferOptions_e.swCutListTransferOptions_CutListProperties, false, "", out longstatus, out longWarnings);

                    System.Diagnostics.
Debugger.Break();

                    
if (boolstatus == false)
                        ErrorMsg(swApp,
"Failed to insert weldment member into new part");

                    swModel = (
ModelDoc2)swApp.ActiveDoc;
                    
if (swModel == null)
                        ErrorMsg(swApp,
"Failed to set open model as active document");

                    updateBoolstatus =
true;
                    
break;
                }
                swFeat = (
Feature)swFeat.GetNextFeature();
            }

            
if (updateBoolstatus == true)
            {
                swFeat = (
Feature)currentModel.FirstFeature();
                
if (swFeat == null)
                    ErrorMsg(swApp,
"Failed to get first feature");

                
while ((swFeat != null))
                {
                    
if (swFeat.GetTypeName2() == "SolidBodyFolder")
                    {
                        swBodyFolder = (
BodyFolder)swFeat.GetSpecificFeature2();
                        
if (swBodyFolder == null)
                            ErrorMsg(swApp,
"Failed to get body folder");

                        boolstatus = swBodyFolder.SetAutomaticCutList(
true);
                        
if (boolstatus == false)
                            ErrorMsg(swApp,
"Failed to set cut list to automatic");

                        boolstatus = swBodyFolder.UpdateCutList();
                        
if (boolstatus == false)
                            ErrorMsg(swApp,
"Failed to update cut list");

                        swApp.CloseDoc(swModel.GetTitle());
                        
break;
                    }
                    swFeat = (
Feature)swFeat.GetNextFeature();
                }
            }

        }

        
public void ErrorMsg(SldWorks Swapp, string Message)
        {
            Swapp.SendMsgToUser2(Message, 0, 0);
            Swapp.RecordLine(
"'*** WARNING - General");
            Swapp.RecordLine(
"'*** " + Message);
            Swapp.RecordLine(
"");
        }



        
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:   Save Solid Body to File 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) 2019 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.