Save Solid Body to File Example (C#)
This example shows how to save a weldment member to another
part document.
//----------------------------------------------------------------------------
// Preconditions: Open a weldment part with a cut list.
//
// Postconditions:
// 1. The cut list in the weldment part is updated.
// 2. The first weldment member in the FeatureManager design tree is saved
to
// RefWeldment1.sldprt, and the cut list properties in the parent
part
// are saved to the cut list of the new part.
// 3. RefWeldment1.sldprt is opened, and its cut list is updated.
// This may take a few minutes.
// 4. When the macro stops, press F5 to close the saved part.
//
---------------------------------------------------------------------------
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;
}
}