RSS

Using Solidworks to convert CAD models

24 May

public static bool SWPartTo3DXML(ISldWorks SwApp, string InputFilepath, string OutputFilePath)
{
int lErrors=0, lWarnings=0;
ModelDoc2 swModel = SwApp. OpenDoc6 (InputFilepath, (int)swDocumentTypes_e.swDocPART,
(int) swOpenDocOptions_e.swOpenDocOptions_LoadModel, “”, ref lErrors, ref lWarnings);

ModelDocExtension swModelDocExt = swModel.Extension;
object swExportData = SwApp.GetExportFileData(1);
return swModelDocExt.SaveAs(OutputFilePath, 0, 0, swExportData, ref lErrors, ref lWarnings);
}

Demo usage:

{
string path = Application.StartupPath;
string File2Open = Path.Combine(path,@”samples\tutorial\30minute\pressure_plate.sldprt”);

//The format is determined by the extension!
string File2Save = “Output.3dxml”; //The same folder as the input;
if (SWPartTo3DXML(SwApp, File2Open, File2Save))
MessageBox.Show(“Success”);
else
MessageBox.Show(“Failure”);
}

When converting non-native format files, use the function LoadFile4 instead:

public static bool AcisTo3DXML(ISldWorks SwApp, string InputFilepath, string OutputFilePath)
{
object swImportData = SwApp.GetImportFileData(InputFilepath);
int lErrors = 0;
//Use LoadFile4 to import non-native format file
ModelDoc2 swModel = SwApp.LoadFile4(InputFilepath, “r”, swImportData, ref lErrors);
return 0 == swModel.SaveAs2(OutputFilePath, 0, true, false);
}

Demo usage:

{
string path = Application.StartupPath;
string File2Open = Path.Combine(path,@”samples\tutorial\30minute\pressure_plate.sat”);

//The format is determined by the extension!
string File2Save = “Output.3dxml”; //The same folder as the input;
if (AcisTo3DXML(SwApp, File2Open, File2Save))
MessageBox.Show(“Success”);
else
MessageBox.Show(“Failure”);
}

Note that the output file is saved in the addin dll folder, instead of the same folder as the input file. This is because Solidworks has not saved the imported SAT file into *.SLDPRT file yet; if it has been saved and then exported, the output file will be in the same folder as the input.

Note that the following function calls seem to be equivilent:

  • swModel.SaveAs2 (This is simpler in code writing)
  • (SwApp.ActiveDoc)).SaveAs2
  • ModelDocExtension.SaveAs
Advertisements
 
2 Comments

Posted by on May 24, 2008 in CAD

 

2 responses to “Using Solidworks to convert CAD models

  1. wavemill

    November 12, 2008 at 1:22 am

    Hi!!

    Thank you for this interesting code.
    Just one question:
    I use api sw2008 and sw2008. No problem to export in iges or step, but impossible to reimport.
    I have an index problem.
    Have you ever have this problem?

    regards,

    Wavemill

     
  2. xinyustudio

    November 17, 2008 at 4:29 pm

    I am not very sure about the import problem you mentioned. You might lose sth when importing some format files, for instance, when importing ACIS file, some (not all) attributes are reserved. And the documents are not clearlly written either. So far, trial and error is inevitable and good luck!

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: