大家好,今天要介紹的是和打包相關(guān)的API,之前講過(guò)一篇文章是關(guān)于打包時(shí)候的注意事項(xiàng),這里就不再介紹了,有需要的家人可以訪問(wèn)前一個(gè)文章:
C# Solidworks二次開發(fā):Pack and Go打包時(shí)需要注意的地方,純干貨(可以節(jié)省大量查找資料時(shí)間)-CSDN博客
下面介紹相關(guān)API:
(1)第一個(gè)為AddExternalDocument,這個(gè)API的含義為將非solidworks文件添加到打包或是移動(dòng)中,下面是官方的具體解釋:
其輸入?yún)?shù)值只有一個(gè)為要添加到打包和移動(dòng)的非solidworks文件的路徑和文件名的數(shù)組,返回值為bool類型,成功為true,失敗為false。
下面是官方使用的例子:
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. Verify that the specified assembly exists.
// 2. Create c:\PackAndGo.
// 3. Open the Immediate window.
//
// Postconditions:
// 1. Gets the names of SOLIDWORKS, render reference, and
//??? non-SOLIDWORKS files for Pack and Go.
// 2. Gets the the name of non-SOLIDWORKS file to remove.
// 3. Packs up SOLIDWORKS, render reference, and
//??? non-SOLIDWORKS files and copies them to c:\PackAndGo.
// 4. Examine c:\PackAndGo and the Immediate window.
//-------------------------------------------
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:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2018\\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:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2018\\samples\\tutorial\\edraw\\claw\\claw-mechanism.easm";
????????????otherFiles[0] = (object)otherFile;
????????????otherFile = "C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2018\\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:\\PackAndGo\\";
????????????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;
????}
}
(2)第二個(gè)為GetDocumentNames,這個(gè)API的含義為獲取打包和移動(dòng)所有文件的名稱,下面是官方的具體解釋:
其輸入?yún)?shù)值只有一個(gè)為要添加到打包和移動(dòng)的非solidworks文件的路徑和文件名的數(shù)組,返回值為bool類型,成功為true,失敗為false。和上面的一致。
(3)第三個(gè)為GetDocumentSaveToNames,這個(gè)API的含義為獲取保存模型文檔的路徑和模型名,下面是官方的具體解釋:
輸入?yún)?shù)有兩個(gè),分別為:
PathNameList
包含保存模型文檔的路徑和文件名的字符串?dāng)?shù)組(參見(jiàn)備注)
DocumentStatusList
包含swPackAndGoDocumentStatus_e中定義的文檔類型的數(shù)組
其對(duì)應(yīng)的類型有三個(gè):文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-854502.html
Member | Description |
---|---|
swPackAndGoDocumentStatus_Normal | 0 = Normal |
swPackAndGoDocumentStatus_UnKnown | 2 = Unknown |
swPackAndGoDocumentStatus_Virtual | 1 = Virtual |
本篇文章要介紹的API就是這些,我們下篇文章再見(jiàn)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-854502.html
到了這里,關(guān)于C# Solidworks二次開發(fā):Pack And Go相關(guān)API詳解(第二講)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!