Solved

Catching Autocad events with Delphi

Posted on 2006-11-09
6
990 Views
Last Modified: 2012-06-21
I'm writing an application with dephi 5 that control Autocad 2005. In term of getting Autocad to draw what I need its working just fine but   I would like my application to be notify when either an application or a document event is trigged like " Application activate" or "Begin save" . I read on the web that I have to write "an event sink generator" for which I need code example. For now my application crash when I try the following code :

 unit Unit2;

interface
uses AutoCAD_TLB,ComObj,windows;
procedure ActivateAcad();
implementation
 var
  AutoCADApplication:IAcadApplication;
  AcadDoc:TAcadDocument;

procedure ActivateAcad();
begin
AutoCADApplication := CreateOleObject('AutoCAD.Application')as IAcadApplication;
AutoCADApplication.Visible := true;

AcadDoc.ConnectTo(AutoCADApplication.ActiveDocument); // This is the problematic line....

end;

end.



0
Comment
Question by:jboivin2
  • 3
  • 2
6 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17911872
Maybe you do it elsewhere, but from the code you list above, you never instantiate an instance of AcadDoc (which I assume is the TOleServer component wrapper).

eg:

 AcadDoc:=TAcadDocument.Create(Application); // Or form, etc, whatever the owner is desired to be
 AcadDoc.ConnectTo(AutoCADApplication.ActiveDocument);
 ....


Regards,
Russell

 
0
 

Author Comment

by:jboivin2
ID: 17913714
Thanks Russell. A good start but what about this event sink generator ?
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 17913897

Show me what the compiler genertated for the TAcadDocument ole server component. Usually you will find that the event sink interface has already been handled and exposed as delphi events.

Russell

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:jboivin2
ID: 17921345
Following your comment I had look at AutoCAD_TLB and  as you said, Delphi eventsare exposed  in the TAcadDocument component wrapper ;
...

TAcadDocument = class(TOleServer)
 
published
{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
    property Server: TAcadDocumentProperties read GetServerProperties;
{$ENDIF}
    property OnBeginSave: TAcadDocumentBeginSave read FOnBeginSave write FOnBeginSave;
    property OnEndSave: TAcadDocumentEndSave read FOnEndSave write FOnEndSave;
    property OnBeginCommand: TAcadDocumentBeginCommand read FOnBeginCommand write FOnBeginCommand;
    property OnEndCommand: TAcadDocumentEndCommand read FOnEndCommand write FOnEndCommand;
    property OnBeginLisp: TAcadDocumentBeginLisp read FOnBeginLisp write FOnBeginLisp;
...
I had therefore drop on my form the TAcadDocument component, which i wasn't using at all up to now, and got only " No such interface supported" messsage when I was setting Autoconnect to true;
I then tried to get the connection form a procedure similar to the one we already discuss:

Procedure ActivateAcad();
begin
        Acad := CreateOleObject('AutoCAD.Application')as IAcadApplication;
        Acad.Visible := true;
        form1.AcadDoc:=TAcadDocument.Create(Application); // not sure this one is necessary
        form1.AcadDoc.ConnectTo(Acad.ActiveDocument);
end;

I did not get any error messages but no result  for :



procedure TForm1.AcadDocActivate(Sender: TObject);
begin

showmessage('youppi');
end;

procedure TForm1.AcadDocWindowMovedOrResized(Sender: TObject;
  HWNDFrame: Integer; bMoved: WordBool);
begin
showmessage('youppi');
end;

procedure TForm1.AcadDocBeginSave(Sender: TObject;
  var FileName: OleVariant);
begin
showmessage('youppi');


What is wrong ?

Regards
Johnny


0
 

Author Comment

by:jboivin2
ID: 17921417
I found what was missing

 procedure ActivateAcad();
begin
        Acad := CreateOleObject('AutoCAD.Application')as IAcadApplication;
        Acad.Visible := true;
        form1.AcadDoc.ConnectTo(Acad.ActiveDocument);
        form1.AcadDoc.OnActivate(application);// was missinn

        end;

Thanks Russel for your guidance
0
 

Expert Comment

by:paulo_psa
ID: 22694171
He is doing what I need but it's not working to me.
They are using D5 and Autocad 5. I'm using D7 Autocad 8.
Things looks similar but doesn't work.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question