SolidWorks API (Delphi): connecting and retrieving an object tree
Probably, many readers have heard about the SolidWorks program ( see offsite ). In this article, we are interested in it from the point of view of automating the development of products (parts and assemblies) at the design stage of industrial production.
The statement of the problem is similar to that described in the Tekla Structure API (c #) article : connecting and receiving a tree of objects , with the exception of the implementation technology, to get a tree of model objects, i.e. assemblies that are part of the model and parts that are part of assemblies, their relationships and parameters.
The development system used is Borland Delphi 7.
SolidWorks has its own api, which comes with the program itself. In the help on api ( here) the classes are described quite well and examples are given in c # and Visual Basic, in general, everything is intuitive, it’s not difficult to understand. Also, with all questions, you can contact your personal account on the SolidWorks portal for support.
If the SolidWorks program is installed correctly, then in Project-> Import Type Library ... we find
Remember to connect SldWorks_TLB and SwConst_TLB in uses to the project.
Here's an article Introducing SolidWorks from Delphi: Connectivity .
Please note that if the SolidWorks program is already running, then we connect to it and open a new document in it; if it is not already running, then we launch it in invisible mode. If for any purpose you need to make running SolidWorks visible, use the following:
A model can have several configurations that are different from each other (with different details, parameters, connections, etc.). As part of the current task, we need to get all the configurations with all their components.
It is possible to get only the currently active configuration:
So, the task is solved.
The statement of the problem is similar to that described in the Tekla Structure API (c #) article : connecting and receiving a tree of objects , with the exception of the implementation technology, to get a tree of model objects, i.e. assemblies that are part of the model and parts that are part of assemblies, their relationships and parameters.
The development system used is Borland Delphi 7.
SolidWorks has its own api, which comes with the program itself. In the help on api ( here) the classes are described quite well and examples are given in c # and Visual Basic, in general, everything is intuitive, it’s not difficult to understand. Also, with all questions, you can contact your personal account on the SolidWorks portal for support.
Connecting SolidWorks Libraries
If the SolidWorks program is installed correctly, then in Project-> Import Type Library ... we find
- SldWorks 2015 Type Library (Version ___)
- SOLIDWORKS 2015 Constant type library (Version ___)
Remember to connect SldWorks_TLB and SwConst_TLB in uses to the project.
Connect to SolidWorks
Here's an article Introducing SolidWorks from Delphi: Connectivity .
procedure SldWrkConnect;
var
swApp: ISldWorks;
fileName,fileExt: String;
fileerror,filewarning: LongInt;
DocType: swDocumentTypes_e;
swModel: IModelDoc2;
begin
fileName := 'C:\Model.SLDASM';
fileExt := AnsiUpperCase(ExtractFileExt(fileName));
if fileExt = '.SLDASM' then
DocType := swDocASSEMBLY //сборка
else
if fileExt = '.SLDPRT' then
DocType := swDocPART //деталь
else
if fileExt = '.SLDDRW' then
DocType := swDocDRAWING //чертеж
else
begin
ShowMessage('Неизвестный тип данных!');
DocType := swDocNONE;
end;
swApp := IUnknown(CreateOleObject('SldWorks.application')) as ISldWorks;
swApp.OpenDoc6(fileName,DocType,swOpenDocOptions_Silent,'',fileerror, filewarning);
swModel := swapp.ActiveDoc as IModelDoc2; //получаем открытую модель
...
//по окончании работы необходимо завершить процесс
if not swApp.Visible then
begin
swApp.CloseAllDocuments(true);
swApp.ExitApp;
end;
end;
Please note that if the SolidWorks program is already running, then we connect to it and open a new document in it; if it is not already running, then we launch it in invisible mode. If for any purpose you need to make running SolidWorks visible, use the following:
swApp.Visible := true;
Getting model parameters
procedure SldWrkConnect;
var
...
author,dataCreate,nameParam: String;
vCustomInfo: OleVariant;
j: Integer;
begin
...
//параметры из вкладки "Суммарная информация"
author := swModel.SummaryInfo[swSumInfoAuthor];
dataCreate := swModel.SummaryInfo[swSumInfoCreateDate];
//параметры из вкладки "Настройка"
vCustomInfo := swModel.GetCustomInfoNames; //получаем все параметры с их значениями
for j := VarArrayLowBound(vCustomInfo,1) to VarArrayHighBound(vCustomInfo,1) do
if 'НАИМ_ПАРАМ' = AnsiUpperCase(vCustomInfo[j]) then //ищем интересующий параметр
nameParam := swModel.GetCustomInfoValue('',vCustomInfo[j]);
...
end;
Retrieving Configurations
A model can have several configurations that are different from each other (with different details, parameters, connections, etc.). As part of the current task, we need to get all the configurations with all their components.
procedure SldWrkConnect;
var
...
vConfArr: array of String;
k: Integer;
swComp: Component2;
swConf: IConfiguration;
begin
...
k := swModel.GetConfigurationCount;
setLength(vConfArr,k);
vConfArr := swModel.GetConfigurationNames;
for i:=0 to k-1 do
begin
swConf := swModel.IGetConfigurationByName(vConfArr[i]);
swComp := swConf.GetRootComponent3(true);
if DocType = swDocASSEMBLY then //если сборка, то получим её дочерние элементы
Traverse(swComp,nil);
end
end;
It is possible to get only the currently active configuration:
swConf:= swModel.IGetActiveConfiguration;
Getting the object tree for configuration
procedure Traverse(swComp:Component2; node:TTreeNode);
var
vChildArr: OleVariant;
i:Integer;
nodeChild: TTreeNode;
begin
if (node = nil) then
nodeChild := TreeView.Items.Add(nil,swComp.Name2)
else
nodeChild := TreeView.Items.AddChild(node,swComp.Name2);
vChildArr:= swComp.GetChildren;
if not VarIsNull(vChildArr) then
for i := VarArrayLowBound(vChildArr, 1) to VarArrayHighBound(vChildArr, 1) do
Traverse(IUnknown(vChildArr[i]) as IComponent2, nodeChild);
end;
So, the task is solved.
Sources Used When Writing Code
- forum.solidworks.com
- api help examples