Home > Back-end >  The function GetOrCreastObject error, solving
The function GetOrCreastObject error, solving

Time:09-19

As title, function GetOrCreastObject error, solving the
This is a test of Solidworks for Delphi development program
Error is shown in figure:

The code is as follows:
[code=Delphi] [unit YSZfprm;

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls ComCtrls ComObj, Activex;

Type
TForm1=class (TForm)
CmBtJr: TButton;
CmBtTc: TButton;
Label1: TLabel;
For: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
CmBtCP: TButton;
CmBtSc: TButton;
EdWz: Tedit;
SaveDialog1: TSaveDialog;
StatusBar1: TStatusBar;
Procedure Button2Click (Sender: TObject);
Procedure Button1Click (Sender: TObject);
Procedure Button3Click (Sender: TObject);
Procedure Button4Click (Sender: TObject);
Private
{Private declarations}
//function GetOrCreastObject (const ClassName: String) : IDispatch;//define functions GetOrCreateObjec, character parameter CLassName, used to deliver ProgID. Returns a selected the original COM object
//according to the operation of SW in the create or obtain SldWorks object//the function description

Public
{Public declarations}
SwApp, Part: the variant;//OLE object variables, corresponding SldWorks and PartDoc object
DocName: string;//DocName to hold to save the document name
The function GetOrCreastObject (const ClassName: String) : IDispatch;//define functions GetOrCreateObjec, character parameter CLassName, used to deliver ProgID. Returns a selected the original COM object
//according to the operation of SW in the create or obtain SldWorks object//the function description
end;

Var
Form1: TForm1;

Implementation

{$R *. DFM}
The function TForm1. GetOrCreastObject (const ClassName: String) : IDispatch;
Var//variable description
Every: TGUID;//define variables for every TGUID type, GUID is a 128 - bit number, it is "globally unique identifiers
"Unknown: IUnknown;//Unknown for IUnknown type, define a variable IUnknown is a base class, all of the COM class must be derived from the base class. Any interface must be implementing the IUnknown interface is not really a COM interface
Between the Begin//Begin in line 12 END parts for execution, it is composed of a series of statements, each statement to perform certain actions, accomplish a certain task, two statements with a semicolon (;) Separated,
Every:=ProgIDToClassID (ClassName);//call ProgIDToClassID return GUID
If Succeeded (GetActiveObject (every nil, Unknown)) then//determine whether the system has enabled the missile destroyer SldWorks object
OleCheck (Unknown. QueryInterface (IDispatch, Result))//has been activated, call OleCheck process
The else
Result:=CreateOleObject (ClassName);//create SldWorks object
end;

procedure TForm1.Button1Click(Sender: TObject);
The begin
If SaveDialog1. Execute Then//open the save dialog box with the Execute method
The Begin
DocName:=SaveDialog1. FileName;//get the file name
Part. SaveAs (DocName);//complete inventory
end;
end;

Procedure TForm1. Button2Click (Sender: TObject);
The begin
SwApp:=GetOrCreateObject (' SldWorks. Application);//use function GetOrCreateObject create or obtain SldWorks object
SwApp. Visible:=True;//to create SldWorks object visible
end;

Procedure TForm1. Button3Click (Sender: TObject);
The begin
SwApp. CloseDoc (DocName);//close the current document
SwApp. ExitApp;//exit SW
Close;//to close the dialog
end;

Procedure TForm1. Button4Click (Sender: TObject);
The begin
Part:=swApp. NewPart;//set up new document
Part. InsertSketchText (0, 0, edWz. Text, 0,0,0,100,100);//object using PartDoc InsertSketchText method to create words
Part. The FeatureManager. FeatureExtrusion (True, False, False, 0,0,0.01, 0.01, False, False, False, False, 0, 0, False, False, False, False, False, False, True);//FeatureExtrusion method on the basis of the text to create stretching matrix, convex platform feature
Part. EditReBuild;//heavy modeling
Part. ViewZoomtofit2//full screen
end;

End.]