Solved

Catching Autocad events with Delphi

Posted on 2006-11-09
6
979 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
Comment Utility
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
Comment Utility
Thanks Russell. A good start but what about this event sink generator ?
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
Comment Utility

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:jboivin2
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now