AutoCAD 控制例子(需要安装 AutoCAD,使用 Variant 方式控制):
procedure TForm1.Button1Click(Sender: TObject);
var
p1, p2, p3: OleVariant; // start & end points of line
Mspace, Acad : OleVariant;
begin
// Create variant arrays to hold coordinates
// VT_R8 = 5; { 8 byte real defined in /Source/RTL/Win/ActiveX.Pas }
p1 := VarArrayCreate([0, 2], VT_R8);
p2 := VarArrayCreate([0, 2], VT_R8);
p3 := VarArrayCreate([0, 2], VT_R8);
// Assign values to array elements
p1[0] := 2.0; p1[1] := 4.0; p1[2] := 0.0;// from 2,4,0
p2[0] := 12.0; p2[1] := 14.0; p2[2] := 0.0; // to 12,14,0
p3[0] := 7.0; p3[1] := 8.0; p3[2] := 0.0;
// Get Application and ModelSpace objects:
try
// see if AutoCAD is already running
Acad := GetActiveOleObject('AutoCAD.Application.14'); // 看清 CAD 版本
except // 如果已经启动
// if it is not running - start it up
Acad:= CreateOleObject('AutoCad.Application.14'); // 还没启动,启动它
end;
// bring AutoCAD to the windows desktop
Acad.visible:= True; // 这才看得见
Mspace := Acad.ActiveDocument.ModelSpace;
// use AutoCAD methods to draw a line and 3 circles
Mspace.AddLine(VarArrayRef(p1), VarArrayRef(p2)).Update;
MSpace.AddCircle(VarArrayRef(p1), 1.5).Update;
MSpace.AddCircle(VarArrayRef(p2), 1).Update;
MSpace.AddCircle(VarArrayRef(p3), 2.0).Update;
// use AutoCAD methods to draw other shapes and text
MSpace.AddArc(VarArrayRef(p3), 1.2, 1, 2).Update;
MSpace.AddBox(VarArrayRef(p2), 5, 3, 2).Update;
MSpace.AddCone(VarArrayRef(p1), 1.3, 2).Update;
MSpace.AddCylinder(VarArrayRef(p3), 1.7, 1.5).Update;
MSpace.AddMtext(VarArrayRef(p3), 10, 'Delphi 5 Rocks!!!').update;
end;