1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComObj,
StdCtrls, AutoCAD_TLB, acconst, ActiveX;
Function SafeArrayRef(V : OleVariant): PSafeArray;
Function GetAcadApplication(start: Boolean): IAcadApplication;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
BtnAutoCAD: TButton;
BtnDirRen: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure BtnAutoCADClick(Sender: TObject);
procedure BtnDirRenClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Acad : OleVariant;
implementation
uses Variants, StrUtils;
{$R *.DFM}
//*****************************************************************************
Function GetAcadApplication(start: Boolean): IAcadApplication;
var
v : OleVariant;
begin
Result := nil;
try
v := GetActiveOleObject('AutoCAD.Application');
Result := IDispatch(v) as IAcadApplication;
except
if start then
begin
Result := CoAcadApplication.Create;
Result.Visible := True;
end;
end;
end;
//*****************************************************************************
Function SafeArrayRef(V : OleVariant): PSafeArray;
begin
Result := PSafeArray(TVarData(V).VArray);
end;
//*****************************************************************************
procedure TForm1.Button1Click(Sender: TObject);
var
Objects, Circle, vPoint, Mspace : OleVariant;
sstext : OleVariant;
FilterType, i, j : Integer;
FilterData : Variant;
AttributeName : String;
begin
// Create the point array and assign values to it
vPoint := VarArrayCreate([0,2], VT_R8);
vPoint[0] := 2.0; vPoint[1] := 4.0; vPoint[2] := 0.0;
// Get the AutoCAD application object
OpenDialog1.Execute;
// Get the ActiveDocument's model space object
Mspace := Acad.ActiveDocument.Modelspace;
for j := 1 to OpenDialog1.Files.Count do
begin
Acad.Documents.Open(OpenDialog1.Files.Strings[j-1]);
Objects := Acad.ActiveDocument.SelectionSets.Add('1');
Objects.Select(acSelectionSetAll);
Memo1.Lines.Add(ExtractFileName(OpenDialog1.Files.Strings[j-1]) + ' ' + IntToStr(Objects.Count));
for i := 0 to Objects.Count - 1 do
begin
if Objects.Item(i).EntityName = 'AcDbAttributeDefinition' then
begin
AttributeName := ExtractFileName(OpenDialog1.Files.Strings[j-1]);
Objects.Item(i).TagString := AnsiReplaceStr(UpperCase(AttributeName),'.DWG','');
Objects.Item(i).UpDate;
Memo1.Lines.Add(Objects.Item(i).TagString + ' - ' + Objects.Item(i).EntityName);
end;
if Objects.Item(i).Layer = 'View_3D' then
Objects.Item(i).delete;
end;
Application.ProcessMessages;
//ShowMessage(IntToStr((Objects.Count)));
Memo1.Lines.Add('New Count : ' + IntToStr(Objects.Count));
Acad.ActiveDocument.PurgeAll;
Acad.ActiveDocument.Save;
Acad.ActiveDocument.Close;
end;
// call the AddCircle() method to create the
// circle with a radius of 10 units:
//Circle := Mspace.AddCircle(VarArrayRef(vPoint), 10.0); // R14.00 only
//Circle := Mspace.AddCircle(Vpoint, 10.0); // R14.01 only
// Update the circle
//Circle.Update;
//FreeAndNil(Acad);
Memo1.Lines.SaveToFile(ExtractFilePath(OpenDialog1.FileName)+ 'export.txt');
end;
//*****************************************************************************
procedure TForm1.Button2Click(Sender: TObject);
var
p1, p2, p3: OleVariant; // start & end points of line
Mspace, Acad, Circle : 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');
except
// if it is not running - start it up
Acad:= CreateOleObject('AutoCad.Application');
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;
Circle := MSpace.AddCylinder(VarArrayRef(p3), 1.7, 1.5).Update;
Circle.mirror(p1,p2);
// MSpace.AddMtext(VarArrayRef(p3), 10, 'Delphi 3 Rocks!!!').update;
end;
//*****************************************************************************
procedure TForm1.Button3Click(Sender: TObject);
var
vPoint : OleVariant;
Acad : AcadApplication;
Mspace : AcadModelSpace;
Circle : AcadCircle;
begin
vPoint := VarArrayCreate([0,2], VT_R8);
vPoint[0] := 2.0; vPoint[1] := 4.0; vPoint[2] := 0.0;
acad := GetAcadApplication(True);
Mspace := Acad.ActiveDocument.ModelSpace;
Circle := Mspace.AddCircle(vPoint, 10.0);
Circle.Update;
end;
//*****************************************************************************
procedure TForm1.BtnAutoCADClick(Sender: TObject);
begin
//Acad:= CreateOleObject('AutoCad.Application');
Acad := GetActiveOleObject('AutoCAD.Application');
end;
//*****************************************************************************
procedure TForm1.BtnDirRenClick(Sender: TObject);
var
j : integer;
NewNameFile, DirNewFile : String;
begin
OpenDialog1.Execute;
for j := 1 to OpenDialog1.Files.Count do
begin
if UpperCase(ExtractFileExt(OpenDialog1.Files.Strings[j-1])) = '.DWG' then
begin
NewNameFile := ExtractFileName(OpenDialog1.Files.Strings[j-1]);
NewNameFile := AnsiReplaceStr(ExtractFileName(OpenDialog1.Files.Strings[j-1]),Edit1.Text,'');
DirNewFile := ExtractFilePath(OpenDialog1.Files.Strings[j-1]);
Memo1.Lines.Add(DirNewFile + NewNameFile);
RenameFile(OpenDialog1.Files.Strings[j-1],DirNewFile + NewNameFile);
end;
end;
end;
end. |
Partager