How to open dwg one by one use delphi 5 and A2K?

How to open dwg one by one use delphi 5 and A2K?

Anonymous
Not applicable
255 Views
12 Replies
Message 1 of 13

How to open dwg one by one use delphi 5 and A2K?

Anonymous
Not applicable
I'm a new comer in delphi. I need open a lot of big dwg file
one by one.In my program I can open one dwg file but I can not
open them one by one. I get a error massage "the call be refused"
(Sorry. Because I used japanese version I can't give the exactly
error message). Is any one can help me or recommend some examples(VB or
delphi) .
By the way, how can I open some dwg files in same time with delphi and
A2K?
thank you!

This is my program.

function MyOpen(DWGFile: String): Boolean;
var
AcadObj: Variant;
acadDoc: Variant;
LayerColl, aLayer, ActLayerObj: Variant;
LayerName, ActiveLayerName : String;

begin
try
AcadObj := GetActiveOleObject('AutoCAD.Application');
except
try
AcadObj := CreateOleObject('AutoCAD.Application');
except
Exit;
end;
end;

AcadObj.Visible := True;
Acadobj.Documents.open(DWGFile);

acadDoc := AcadObj.ActiveDocument;
// acadDoc.Open(DWGFile);
LayerColl := acadDoc.Layers;
ActLayerObj := acadDoc.ActiveLayer;
ActiveLayerName := ActLayerObj.Name;
//to freeze every layer without '0'
for cnt := 0 to LayerColl.Count - 1 do
begin
aLayer := LayerColl.Item(cnt);
LayerName := aLayer.Name;
if ( ActiveLayerName <> '0') and ( LayerName = '0') then
acadDoc.ActiveLayer := aLayer;
if LayerName <> '0' then
aLayer.Freeze := True;
end;

// other process

end;
// for test
procedure TForm1.Button1Click(Sender: TObject);
begin
MyOpen('01.DWG');
MyOpen('02.DWG');
MyOpen('03.DWG');
end;
0 Likes
256 Views
12 Replies
Replies (12)
Message 2 of 13

Anonymous
Not applicable
Can you tell me on what line of your code you get the error?

zhouzuo wrote:
>
> I'm a new comer in delphi. I need open a lot of big dwg file
> one by one.In my program I can open one dwg file but I can not
> open them one by one. I get a error massage "the call be refused"
> (Sorry. Because I used japanese version I can't give the exactly
> error message). Is any one can help me or recommend some examples(VB or
> delphi) .
> By the way, how can I open some dwg files in same time with delphi and
> A2K?
> thank you!
>
> This is my program.
>
> function MyOpen(DWGFile: String): Boolean;
> var
> AcadObj: Variant;
> acadDoc: Variant;
> LayerColl, aLayer, ActLayerObj: Variant;
> LayerName, ActiveLayerName : String;
>
> begin
> try
> AcadObj := GetActiveOleObject('AutoCAD.Application');
> except
> try
> AcadObj := CreateOleObject('AutoCAD.Application');
> except
> Exit;
> end;
> end;
>
> AcadObj.Visible := True;
> Acadobj.Documents.open(DWGFile);
>
> acadDoc := AcadObj.ActiveDocument;
> // acadDoc.Open(DWGFile);
> LayerColl := acadDoc.Layers;
> ActLayerObj := acadDoc.ActiveLayer;
> ActiveLayerName := ActLayerObj.Name;
> //to freeze every layer without '0'
> for cnt := 0 to LayerColl.Count - 1 do
> begin
> aLayer := LayerColl.Item(cnt);
> LayerName := aLayer.Name;
> if ( ActiveLayerName <> '0') and ( LayerName = '0') then
> acadDoc.ActiveLayer := aLayer;
> if LayerName <> '0' then
> aLayer.Freeze := True;
> end;
>
> // other process
>
> end;
> // for test
> procedure TForm1.Button1Click(Sender: TObject);
> begin
> MyOpen('01.DWG');
> MyOpen('02.DWG');
> MyOpen('03.DWG');
> end;

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 3 of 13

Anonymous
Not applicable
Thank you !

My program is stop on
LayerName := aLayer.Name;

My dwg size is gigger than 1500k.

ZhouZuo

Tony Tanzillo wrote:

> Can you tell me on what line of your code you get the error?
>
> zhouzuo wrote:
> >
> > I'm a new comer in delphi. I need open a lot of big dwg file
> > one by one.In my program I can open one dwg file but I can not
> > open them one by one. I get a error massage "the call be refused"
> > (Sorry. Because I used japanese version I can't give the exactly
> > error message). Is any one can help me or recommend some examples(VB or
> > delphi) .
> > By the way, how can I open some dwg files in same time with delphi and
> > A2K?
> > thank you!
> >
> > This is my program.
> >
> > function MyOpen(DWGFile: String): Boolean;
> > var
> > AcadObj: Variant;
> > acadDoc: Variant;
> > LayerColl, aLayer, ActLayerObj: Variant;
> > LayerName, ActiveLayerName : String;
> >
> > begin
> > try
> > AcadObj := GetActiveOleObject('AutoCAD.Application');
> > except
> > try
> > AcadObj := CreateOleObject('AutoCAD.Application');
> > except
> > Exit;
> > end;
> > end;
> >
> > AcadObj.Visible := True;
> > Acadobj.Documents.open(DWGFile);
> >
> > acadDoc := AcadObj.ActiveDocument;
> > // acadDoc.Open(DWGFile);
> > LayerColl := acadDoc.Layers;
> > ActLayerObj := acadDoc.ActiveLayer;
> > ActiveLayerName := ActLayerObj.Name;
> > //to freeze every layer without '0'
> > for cnt := 0 to LayerColl.Count - 1 do
> > begin
> > aLayer := LayerColl.Item(cnt);
> > LayerName := aLayer.Name;
> > if ( ActiveLayerName <> '0') and ( LayerName = '0') then
> > acadDoc.ActiveLayer := aLayer;
> > if LayerName <> '0' then
> > aLayer.Freeze := True;
> > end;
> >
> > // other process
> >
> > end;
> > // for test
> > procedure TForm1.Button1Click(Sender: TObject);
> > begin
> > MyOpen('01.DWG');
> > MyOpen('02.DWG');
> > MyOpen('03.DWG');
> > end;
>
> --
> /*********************************************************/
> /* Tony Tanzillo Design Automation Consulting */
> /* Programming & Customization for AutoCAD & Compatibles */
> /* ----------------------------------------------------- */
> /* tony.tanzillo@worldnet.att.net */
> /* http://ourworld.compuserve.com/homepages/tonyt */
> /*********************************************************/
0 Likes
Message 4 of 13

Anonymous
Not applicable
The error occurs because the Layer property of an object
is not a Layer object, it is a string that contains the name
of the object's layer.

You only have to change the line of code to this:

LayerName := aLayer;

zhouzuo wrote:
>
> Thank you !
>
> My program is stop on
> LayerName := aLayer.Name;
>
> My dwg size is gigger than 1500k.
>
--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 5 of 13

Anonymous
Not applicable
Thank you for the help!

I changed my program but it does not work.The error is
"Variant be changed into wrong type"(in japanese) on line
LayerName := aLayer;

The problem is happening when I used the big dwg. Are there
some mothod let me to wait for one dwg be process over? Or there are
something wrong in save dwg. I use
acadDoc.save;

I so sorry to trbouble you.

Tony Tanzillo wrote:

> The error occurs because the Layer property of an object
> is not a Layer object, it is a string that contains the name
> of the object's layer.
>
> You only have to change the line of code to this:
>
> LayerName := aLayer;
>
> zhouzuo wrote:
> >
> > Thank you !
> >
> > My program is stop on
> > LayerName := aLayer.Name;
> >
> > My dwg size is gigger than 1500k.
> >
> --
> /*********************************************************/
> /* Tony Tanzillo Design Automation Consulting */
> /* Programming & Customization for AutoCAD & Compatibles */
> /* ----------------------------------------------------- */
> /* tony.tanzillo@worldnet.att.net */
> /* http://ourworld.compuserve.com/homepages/tonyt */
> /*********************************************************/
0 Likes
Message 6 of 13

Anonymous
Not applicable
Sorry, I thought you were processing a collection
of AutoCAD entities, not layers. In that case,
disregard the last instructions, as it is wrong.

Try replacing the line "LayerName := aLayer.Name:"
with the following code, and tell me what you see
displayed in the message box:

try
LayerName := aLayer.Name;
except On e: OleSysError do
ShowMessage(Format("ErrorCode = %d", [e.ErrorCode]));
end;

zhouzuo wrote:
>
> Thank you for the help!
>
> I changed my program but it does not work.The error is
> "Variant be changed into wrong type"(in japanese) on line
> LayerName := aLayer;
>
> The problem is happening when I used the big dwg. Are there
> some mothod let me to wait for one dwg be process over? Or there are
> something wrong in save dwg. I use
> acadDoc.save;
>
> I so sorry to trbouble you.
>
> Tony Tanzillo wrote:
>
> > The error occurs because the Layer property of an object
> > is not a Layer object, it is a string that contains the name
> > of the object's layer.
> >
> > You only have to change the line of code to this:
> >
> > LayerName := aLayer;
> >
> > zhouzuo wrote:
> > >
> > > Thank you !
> > >
> > > My program is stop on
> > > LayerName := aLayer.Name;
> > >
> > > My dwg size is gigger than 1500k.
> > >
> > --
> > /*********************************************************/
> > /* Tony Tanzillo Design Automation Consulting */
> > /* Programming & Customization for AutoCAD & Compatibles */
> > /* ----------------------------------------------------- */
> > /* tony.tanzillo@worldnet.att.net */
> > /* http://ourworld.compuserve.com/homepages/tonyt */
> > /*********************************************************/

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 7 of 13

Anonymous
Not applicable
Thank you for the help!

The error code is
"ErrorCode = -2147418111 "
Some time the programe stop before the line
LayerName := aLayer.Name;
but the error code is same.

By the way I test the program in "SDI" mode.

ZhouZuo

Tony Tanzillo wrote:

> Sorry, I thought you were processing a collection
> of AutoCAD entities, not layers. In that case,
> disregard the last instructions, as it is wrong.
>
> Try replacing the line "LayerName := aLayer.Name:"
> with the following code, and tell me what you see
> displayed in the message box:
>
> try
> LayerName := aLayer.Name;
> except On e: OleSysError do
> ShowMessage(Format("ErrorCode = %d", [e.ErrorCode]));
> end;
>
> zhouzuo wrote:
> >
> > Thank you for the help!
> >
> > I changed my program but it does not work.The error is
> > "Variant be changed into wrong type"(in japanese) on line
> > LayerName := aLayer;
> >
> > The problem is happening when I used the big dwg. Are there
> > some mothod let me to wait for one dwg be process over? Or there are
> > something wrong in save dwg. I use
> > acadDoc.save;
> >
> > I so sorry to trbouble you.
> >
> > Tony Tanzillo wrote:
> >
> > > The error occurs because the Layer property of an object
> > > is not a Layer object, it is a string that contains the name
> > > of the object's layer.
> > >
> > > You only have to change the line of code to this:
> > >
> > > LayerName := aLayer;
> > >
> > > zhouzuo wrote:
> > > >
> > > > Thank you !
> > > >
> > > > My program is stop on
> > > > LayerName := aLayer.Name;
> > > >
> > > > My dwg size is gigger than 1500k.
> > > >
> > > --
> > > /*********************************************************/
> > > /* Tony Tanzillo Design Automation Consulting */
> > > /* Programming & Customization for AutoCAD & Compatibles */
> > > /* ----------------------------------------------------- */
> > > /* tony.tanzillo@worldnet.att.net */
> > > /* http://ourworld.compuserve.com/homepages/tonyt */
> > > /*********************************************************/
>
> --
> /*********************************************************/
> /* Tony Tanzillo Design Automation Consulting */
> /* Programming & Customization for AutoCAD & Compatibles */
> /* ----------------------------------------------------- */
> /* tony.tanzillo@worldnet.att.net */
> /* http://ourworld.compuserve.com/homepages/tonyt */
> /*********************************************************/
0 Likes
Message 8 of 13

Anonymous
Not applicable
Try modifying your code like so:

function MyOpen(DWGFile: String): Boolean;
var
AcadObj: Variant;
acadDoc: Variant;
LayerColl, aLayer, ActLayerObj: Variant;
LayerName, ActiveLayerName : String;

begin
try
AcadObj := GetActiveOleObject('AutoCAD.Application');
except
try
AcadObj := CreateOleObject('AutoCAD.Application');
except
Exit;
end;
end;

AcadObj.Visible := True;
Acadobj.Documents.open(DWGFile);

While (True) do
begin
try
acadDoc := AcadObj.ActiveDocument;
except
Application.ProcessMessages;
continue;
end;
break;
end;
LayerColl := acadDoc.Layers;
ActLayerObj := acadDoc.ActiveLayer;

zhouzuo wrote:
>
> Thank you for the help!
>
> The error code is
> "ErrorCode = -2147418111 "
> Some time the programe stop before the line
> LayerName := aLayer.Name;
> but the error code is same.
>
> By the way I test the program in "SDI" mode.
>
> ZhouZuo
>
> Tony Tanzillo wrote:
>
> > Sorry, I thought you were processing a collection
> > of AutoCAD entities, not layers. In that case,
> > disregard the last instructions, as it is wrong.
> >
> > Try replacing the line "LayerName := aLayer.Name:"
> > with the following code, and tell me what you see
> > displayed in the message box:
> >
> > try
> > LayerName := aLayer.Name;
> > except On e: OleSysError do
> > ShowMessage(Format("ErrorCode = %d", [e.ErrorCode]));
> > end;
> >
> > zhouzuo wrote:
> > >
> > > Thank you for the help!
> > >
> > > I changed my program but it does not work.The error is
> > > "Variant be changed into wrong type"(in japanese) on line
> > > LayerName := aLayer;
> > >
> > > The problem is happening when I used the big dwg. Are there
> > > some mothod let me to wait for one dwg be process over? Or there are
> > > something wrong in save dwg. I use
> > > acadDoc.save;
> > >
> > > I so sorry to trbouble you.
> > >
> > > Tony Tanzillo wrote:
> > >
> > > > The error occurs because the Layer property of an object
> > > > is not a Layer object, it is a string that contains the name
> > > > of the object's layer.
> > > >
> > > > You only have to change the line of code to this:
> > > >
> > > > LayerName := aLayer;
> > > >
> > > > zhouzuo wrote:
> > > > >
> > > > > Thank you !
> > > > >
> > > > > My program is stop on
> > > > > LayerName := aLayer.Name;
> > > > >
> > > > > My dwg size is gigger than 1500k.
> > > > >
> > > > --
> > > > /*********************************************************/
> > > > /* Tony Tanzillo Design Automation Consulting */
> > > > /* Programming & Customization for AutoCAD & Compatibles */
> > > > /* ----------------------------------------------------- */
> > > > /* tony.tanzillo@worldnet.att.net */
> > > > /* http://ourworld.compuserve.com/homepages/tonyt */
> > > > /*********************************************************/
> >
> > --
> > /*********************************************************/
> > /* Tony Tanzillo Design Automation Consulting */
> > /* Programming & Customization for AutoCAD & Compatibles */
> > /* ----------------------------------------------------- */
> > /* tony.tanzillo@worldnet.att.net */
> > /* http://ourworld.compuserve.com/homepages/tonyt */
> > /*********************************************************/

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 9 of 13

Anonymous
Not applicable
Thank you Tony!

I modifed my program but it still have same problem.
I found the While loop do not work. I put a break point
on "Application.ProcessMessages" but is do not stop whatever
it wrong or it ok.
If you put the break point on
"acadDoc := AcadObj.ActiveDocument;"
the program will doing very well.

Can you tell me where I can found meaning of error code?
I'm so sorry for take you so mach time.

Tony Tanzillo wrote:

> Try modifying your code like so:
>
> function MyOpen(DWGFile: String): Boolean;
> var
> AcadObj: Variant;
> acadDoc: Variant;
> LayerColl, aLayer, ActLayerObj: Variant;
> LayerName, ActiveLayerName : String;
>
> begin
> try
> AcadObj := GetActiveOleObject('AutoCAD.Application');
> except
> try
> AcadObj := CreateOleObject('AutoCAD.Application');
> except
> Exit;
> end;
> end;
>
> AcadObj.Visible := True;
> Acadobj.Documents.open(DWGFile);
>
> While (True) do
> begin
> try
> acadDoc := AcadObj.ActiveDocument;
> except
> Application.ProcessMessages;
> continue;
> end;
> break;
> end;
> LayerColl := acadDoc.Layers;
> ActLayerObj := acadDoc.ActiveLayer;
>
> zhouzuo wrote:
> >
> > Thank you for the help!
> >
> > The error code is
> > "ErrorCode = -2147418111 "
> > Some time the programe stop before the line
> > LayerName := aLayer.Name;
> > but the error code is same.
> >
> > By the way I test the program in "SDI" mode.
> >
> > ZhouZuo
> >
> > Tony Tanzillo wrote:
> >
> > > Sorry, I thought you were processing a collection
> > > of AutoCAD entities, not layers. In that case,
> > > disregard the last instructions, as it is wrong.
> > >
> > > Try replacing the line "LayerName := aLayer.Name:"
> > > with the following code, and tell me what you see
> > > displayed in the message box:
> > >
> > > try
> > > LayerName := aLayer.Name;
> > > except On e: OleSysError do
> > > ShowMessage(Format("ErrorCode = %d", [e.ErrorCode]));
> > > end;
> > >
> > > zhouzuo wrote:
> > > >
> > > > Thank you for the help!
> > > >
> > > > I changed my program but it does not work.The error is
> > > > "Variant be changed into wrong type"(in japanese) on line
> > > > LayerName := aLayer;
> > > >
> > > > The problem is happening when I used the big dwg. Are there
> > > > some mothod let me to wait for one dwg be process over? Or there are
> > > > something wrong in save dwg. I use
> > > > acadDoc.save;
> > > >
> > > > I so sorry to trbouble you.
> > > >
> > > > Tony Tanzillo wrote:
> > > >
> > > > > The error occurs because the Layer property of an object
> > > > > is not a Layer object, it is a string that contains the name
> > > > > of the object's layer.
> > > > >
> > > > > You only have to change the line of code to this:
> > > > >
> > > > > LayerName := aLayer;
> > > > >
> > > > > zhouzuo wrote:
> > > > > >
> > > > > > Thank you !
> > > > > >
> > > > > > My program is stop on
> > > > > > LayerName := aLayer.Name;
> > > > > >
> > > > > > My dwg size is gigger than 1500k.
> > > > > >
> > > > > --
> > > > > /*********************************************************/
> > > > > /* Tony Tanzillo Design Automation Consulting */
> > > > > /* Programming & Customization for AutoCAD & Compatibles */
> > > > > /* ----------------------------------------------------- */
> > > > > /* tony.tanzillo@worldnet.att.net */
> > > > > /* http://ourworld.compuserve.com/homepages/tonyt */
> > > > > /*********************************************************/
> > >
> > > --
> > > /*********************************************************/
> > > /* Tony Tanzillo Design Automation Consulting */
> > > /* Programming & Customization for AutoCAD & Compatibles */
> > > /* ----------------------------------------------------- */
> > > /* tony.tanzillo@worldnet.att.net */
> > > /* http://ourworld.compuserve.com/homepages/tonyt */
> > > /*********************************************************/
>
> --
> /*********************************************************/
> /* Tony Tanzillo Design Automation Consulting */
> /* Programming & Customization for AutoCAD & Compatibles */
> /* ----------------------------------------------------- */
> /* tony.tanzillo@worldnet.att.net */
> /* http://ourworld.compuserve.com/homepages/tonyt */
> /*********************************************************/
0 Likes
Message 10 of 13

Anonymous
Not applicable
The error code is telling you that AutoCAD is not available
to service Automation calls. This may be because the Open()
method is not waiting until it is before it returns, and
your next attempt to call into AutoCAD is failing because
AutoCAD is not quiescent.

After you call the Open() method, try inserting the following:

While(not AcadObj.GetAcadState.IsQuiescent) do
Application.ProcessMessages;

zhouzuo wrote:
>
> Thank you Tony!
>
> I modifed my program but it still have same problem.
> I found the While loop do not work. I put a break point
> on "Application.ProcessMessages" but is do not stop whatever
> it wrong or it ok.
> If you put the break point on
> "acadDoc := AcadObj.ActiveDocument;"
> the program will doing very well.
>
> Can you tell me where I can found meaning of error code?
> I'm so sorry for take you so mach time.
>
> Tony Tanzillo wrote:
>
> > Try modifying your code like so:
> >
> > function MyOpen(DWGFile: String): Boolean;
> > var
> > AcadObj: Variant;
> > acadDoc: Variant;
> > LayerColl, aLayer, ActLayerObj: Variant;
> > LayerName, ActiveLayerName : String;
> >
> > begin
> > try
> > AcadObj := GetActiveOleObject('AutoCAD.Application');
> > except
> > try
> > AcadObj := CreateOleObject('AutoCAD.Application');
> > except
> > Exit;
> > end;
> > end;
> >
> > AcadObj.Visible := True;
> > Acadobj.Documents.open(DWGFile);
> >
> > While (True) do
> > begin
> > try
> > acadDoc := AcadObj.ActiveDocument;
> > except
> > Application.ProcessMessages;
> > continue;
> > end;
> > break;
> > end;
> > LayerColl := acadDoc.Layers;
> > ActLayerObj := acadDoc.ActiveLayer;
> >
> > zhouzuo wrote:
> > >
> > > Thank you for the help!
> > >
> > > The error code is
> > > "ErrorCode = -2147418111 "
> > > Some time the programe stop before the line
> > > LayerName := aLayer.Name;
> > > but the error code is same.
> > >
> > > By the way I test the program in "SDI" mode.
> > >
> > > ZhouZuo
> > >
> > > Tony Tanzillo wrote:
> > >
> > > > Sorry, I thought you were processing a collection
> > > > of AutoCAD entities, not layers. In that case,
> > > > disregard the last instructions, as it is wrong.
> > > >
> > > > Try replacing the line "LayerName := aLayer.Name:"
> > > > with the following code, and tell me what you see
> > > > displayed in the message box:
> > > >
> > > > try
> > > > LayerName := aLayer.Name;
> > > > except On e: OleSysError do
> > > > ShowMessage(Format("ErrorCode = %d", [e.ErrorCode]));
> > > > end;
> > > >
> > > > zhouzuo wrote:
> > > > >
> > > > > Thank you for the help!
> > > > >
> > > > > I changed my program but it does not work.The error is
> > > > > "Variant be changed into wrong type"(in japanese) on line
> > > > > LayerName := aLayer;
> > > > >
> > > > > The problem is happening when I used the big dwg. Are there
> > > > > some mothod let me to wait for one dwg be process over? Or there are
> > > > > something wrong in save dwg. I use
> > > > > acadDoc.save;
> > > > >
> > > > > I so sorry to trbouble you.
> > > > >
> > > > > Tony Tanzillo wrote:
> > > > >
> > > > > > The error occurs because the Layer property of an object
> > > > > > is not a Layer object, it is a string that contains the name
> > > > > > of the object's layer.
> > > > > >
> > > > > > You only have to change the line of code to this:
> > > > > >
> > > > > > LayerName := aLayer;
> > > > > >
> > > > > > zhouzuo wrote:
> > > > > > >
> > > > > > > Thank you !
> > > > > > >
> > > > > > > My program is stop on
> > > > > > > LayerName := aLayer.Name;
> > > > > > >
> > > > > > > My dwg size is gigger than 1500k.
> > > > > > >
> > > > > > --
> > > > > > /*********************************************************/
> > > > > > /* Tony Tanzillo Design Automation Consulting */
> > > > > > /* Programming & Customization for AutoCAD & Compatibles */
> > > > > > /* ----------------------------------------------------- */
> > > > > > /* tony.tanzillo@worldnet.att.net */
> > > > > > /* http://ourworld.compuserve.com/homepages/tonyt */
> > > > > > /*********************************************************/
> > > >
> > > > --
> > > > /*********************************************************/
> > > > /* Tony Tanzillo Design Automation Consulting */
> > > > /* Programming & Customization for AutoCAD & Compatibles */
> > > > /* ----------------------------------------------------- */
> > > > /* tony.tanzillo@worldnet.att.net */
> > > > /* http://ourworld.compuserve.com/homepages/tonyt */
> > > > /*********************************************************/
> >
> > --
> > /*********************************************************/
> > /* Tony Tanzillo Design Automation Consulting */
> > /* Programming & Customization for AutoCAD & Compatibles */
> > /* ----------------------------------------------------- */
> > /* tony.tanzillo@worldnet.att.net */
> > /* http://ourworld.compuserve.com/homepages/tonyt */
> > /*********************************************************/

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 11 of 13

Anonymous
Not applicable
Thanks for new suggestion!

I changed my code as you told me but it does not
work . Maybe the problem is not in AcadObj (application),
it is in acadDoc (ActiveDocument). Maybe I have to use
different method to open DWG in SDI mode.
Now I changed my A2K into MDI mode.It work now!
Would you tell me how can I not display the error message when "except" happen.
for example
AcadObj := GetActiveOleObject('AutoCAD.Application');
except
(in VB,can use Err.clear)
Thanks.

Here is my code now.Just for MDI.

function MyOpen(DWGFile: String): Boolean;
var
AcadObj: Variant;
acadDoc: Variant;
LayerColl, aLayer, ActLayerObj: Variant;
LayerName, ActiveLayerName : String;

F: TextFile;
S: string;
cnt: integer;
//for judge of SDI or MDI
sysVarName :String;
varData : Variant;

begin
try
AcadObj := GetActiveOleObject('AutoCAD.Application');
except
try
AcadObj := CreateOleObject('AutoCAD.Application');
except
Exit;
end;
end;

AcadObj.Visible := True;
Acadobj.Documents.close;
Acadobj.Documents.open(DWGFile);

While (True) do
begin
try
acadDoc := AcadObj.ActiveDocument;
except
Application.ProcessMessages;
continue;
end;
break;
end;

sysVarName := 'SDI';
varData := AcadDoc.GetVariable(sysVarName);//no use

LayerColl := acadDoc.Layers;
ActLayerObj := acadDoc.ActiveLayer;

for cnt := 0 to LayerColl.Count - 1 do
begin
aLayer := LayerColl.Item(cnt);
$B!!(BLayerName := aLayer.Name;
if ( ActiveLayerName <> '0') and ( LayerName = '0') then
acadDoc.ActiveLayer := aLayer;
if LayerName <> '0' then
aLayer.Freeze := True;
end;

if varData = 1 then
acadDoc.save
else
Begin
if AcadDoc.Saved = TRUE then
AcadDoc.save;
AcadDoc.close;
end;

end;

Tony Tanzillo wrote:

> The error code is telling you that AutoCAD is not available
> to service Automation calls. This may be because the Open()
> method is not waiting until it is before it returns, and
> your next attempt to call into AutoCAD is failing because
> AutoCAD is not quiescent.
>
> After you call the Open() method, try inserting the following:
>
> While(not AcadObj.GetAcadState.IsQuiescent) do
> Application.ProcessMessages;
>
0 Likes
Message 12 of 13

Anonymous
Not applicable
If you use try...except...end to catch errors, then they
should not appear, unless there is a second error that
occurs in the code appearing between except and end.

zhouzuo wrote:
>
> Thanks for new suggestion!
>
> I changed my code as you told me but it does not
> work . Maybe the problem is not in AcadObj (application),
> it is in acadDoc (ActiveDocument). Maybe I have to use
> different method to open DWG in SDI mode.
> Now I changed my A2K into MDI mode.It work now!
> Would you tell me how can I not display the error message when "except" happen.
> for example
> AcadObj := GetActiveOleObject('AutoCAD.Application');
> except
> (in VB,can use Err.clear)
> Thanks.
>
> Here is my code now.Just for MDI.
>
> function MyOpen(DWGFile: String): Boolean;
> var
> AcadObj: Variant;
> acadDoc: Variant;
> LayerColl, aLayer, ActLayerObj: Variant;
> LayerName, ActiveLayerName : String;
>
> F: TextFile;
> S: string;
> cnt: integer;
> //for judge of SDI or MDI
> sysVarName :String;
> varData : Variant;
>
> begin
> try
> AcadObj := GetActiveOleObject('AutoCAD.Application');
> except
> try
> AcadObj := CreateOleObject('AutoCAD.Application');
> except
> Exit;
> end;
> end;
>
> AcadObj.Visible := True;
> Acadobj.Documents.close;
> Acadobj.Documents.open(DWGFile);
>
> While (True) do
> begin
> try
> acadDoc := AcadObj.ActiveDocument;
> except
> Application.ProcessMessages;
> continue;
> end;
> break;
> end;
>
> sysVarName := 'SDI';
> varData := AcadDoc.GetVariable(sysVarName);//no use
>
> LayerColl := acadDoc.Layers;
> ActLayerObj := acadDoc.ActiveLayer;
>
> for cnt := 0 to LayerColl.Count - 1 do
> begin
> aLayer := LayerColl.Item(cnt);
> $B!!(BLayerName := aLayer.Name;
> if ( ActiveLayerName <> '0') and ( LayerName = '0') then
> acadDoc.ActiveLayer := aLayer;
> if LayerName <> '0' then
> aLayer.Freeze := True;
> end;
>
> if varData = 1 then
> acadDoc.save
> else
> Begin
> if AcadDoc.Saved = TRUE then
> AcadDoc.save;
> AcadDoc.close;
> end;
>
> end;
>
> Tony Tanzillo wrote:
>
> > The error code is telling you that AutoCAD is not available
> > to service Automation calls. This may be because the Open()
> > method is not waiting until it is before it returns, and
> > your next attempt to call into AutoCAD is failing because
> > AutoCAD is not quiescent.
> >
> > After you call the Open() method, try inserting the following:
> >
> > While(not AcadObj.GetAcadState.IsQuiescent) do
> > Application.ProcessMessages;
> >

--
/*********************************************************/
/* Tony Tanzillo Design Automation Consulting */
/* Programming & Customization for AutoCAD & Compatibles */
/* ----------------------------------------------------- */
/* tony.tanzillo@worldnet.att.net */
/* http://ourworld.compuserve.com/homepages/tonyt */
/*********************************************************/
0 Likes
Message 13 of 13

Anonymous
Not applicable
Thank you for all the help!

I'll let mo program allways run under MDI mode.

Zhou Zuo

Tony Tanzillo wrote:

> If you use try...except...end to catch errors, then they
> should not appear, unless there is a second error that
> occurs in the code appearing between except and end.
>
> zhouzuo wrote:
> >
> > Thanks for new suggestion!
> >
> > I changed my code as you told me but it does not
> > work . Maybe the problem is not in AcadObj (application),
> > it is in acadDoc (ActiveDocument). Maybe I have to use
> > different method to open DWG in SDI mode.
> > Now I changed my A2K into MDI mode.It work now!
> > Would you tell me how can I not display the error message when "except" happen.
> > for example
> > AcadObj := GetActiveOleObject('AutoCAD.Application');
> > except
> > (in VB,can use Err.clear)
> > Thanks.
> >
> > Here is my code now.Just for MDI.
> >
> > function MyOpen(DWGFile: String): Boolean;
> > var
> > AcadObj: Variant;
> > acadDoc: Variant;
> > LayerColl, aLayer, ActLayerObj: Variant;
> > LayerName, ActiveLayerName : String;
> >
> > F: TextFile;
> > S: string;
> > cnt: integer;
> > //for judge of SDI or MDI
> > sysVarName :String;
> > varData : Variant;
> >
> > begin
> > try
> > AcadObj := GetActiveOleObject('AutoCAD.Application');
> > except
> > try
> > AcadObj := CreateOleObject('AutoCAD.Application');
> > except
> > Exit;
> > end;
> > end;
> >
> > AcadObj.Visible := True;
> > Acadobj.Documents.close;
> > Acadobj.Documents.open(DWGFile);
> >
> > While (True) do
> > begin
> > try
> > acadDoc := AcadObj.ActiveDocument;
> > except
> > Application.ProcessMessages;
> > continue;
> > end;
> > break;
> > end;
> >
> > sysVarName := 'SDI';
> > varData := AcadDoc.GetVariable(sysVarName);//no use
> >
> > LayerColl := acadDoc.Layers;
> > ActLayerObj := acadDoc.ActiveLayer;
> >
> > for cnt := 0 to LayerColl.Count - 1 do
> > begin
> > aLayer := LayerColl.Item(cnt);
> > $B!!(BLayerName := aLayer.Name;
> > if ( ActiveLayerName <> '0') and ( LayerName = '0') then
> > acadDoc.ActiveLayer := aLayer;
> > if LayerName <> '0' then
> > aLayer.Freeze := True;
> > end;
> >
> > if varData = 1 then
> > acadDoc.save
> > else
> > Begin
> > if AcadDoc.Saved = TRUE then
> > AcadDoc.save;
> > AcadDoc.close;
> > end;
> >
> > end;
> >
> > Tony Tanzillo wrote:
> >
> > > The error code is telling you that AutoCAD is not available
> > > to service Automation calls. This may be because the Open()
> > > method is not waiting until it is before it returns, and
> > > your next attempt to call into AutoCAD is failing because
> > > AutoCAD is not quiescent.
> > >
> > > After you call the Open() method, try inserting the following:
> > >
> > > While(not AcadObj.GetAcadState.IsQuiescent) do
> > > Application.ProcessMessages;
> > >
>
> --
> /*********************************************************/
> /* Tony Tanzillo Design Automation Consulting */
> /* Programming & Customization for AutoCAD & Compatibles */
> /* ----------------------------------------------------- */
> /* tony.tanzillo@worldnet.att.net */
> /* http://ourworld.compuserve.com/homepages/tonyt */
> /*********************************************************/
0 Likes