📜 ⬆️ ⬇️

SolidWorks API (Delphi): connecting and getting an object tree

Probably many readers have heard of the SolidWorks program ( see of.site ). In this article, it interests us from the point of view of automating the development of products (parts and assemblies) at the design stage of industrial production.

Setting a task 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, is to obtain a tree of model objects, i.e. assemblies that are included in the model and parts that are included in the assembly, their relationships and parameters.

The development system used is Borland Delphi 7.

SolidWorks has its api, which comes with the program itself. In the api help ( here ), the classes are pretty well described and examples are given in c # and Visual Basic, in general, everything is intuitive, it's not hard to figure out. Also, with all questions, you can ask for support in your personal account on the SolidWorks portal.
')

Connecting SolidWorks Libraries


If the SolidWorks software is installed correctly, then in the Project-> Import Type Library ... we find

Do not forget to connect SldWorks_TLB and SwConst_TLB in uses to the project.

Connect to SolidWorks


The article helped here. We are introduced to SolidWorks from Delphi: Connection .
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 software is already running, then we connect to it and open a new document in it, but if it is not already running, then we launch it in invisible mode. If for any purpose you want 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; 

Getting configurations


A model may have several different configurations (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 receive 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 problem is solved.

Sources Used When Writing Code


  1. forum.solidworks.com
  2. api help, examples

Source: https://habr.com/ru/post/263133/


All Articles