How to wait for AutoCAD document to finish loading before proceeding in code

조회 수: 8 (최근 30일)
Hi,
We have a Matlab application creating and working with AutoCAD drawings through COM interface. This is buggy, sometimes it works and mostly not. I have tracked down the issue to the following:
When creating the drawing, I have tried to add pause at different ranges before proceeding in the code, but this does not seem to be sufficient. The issue we are facing is that the COM object (AutoCAD drawing document) does not load correctly or fully. I have verfied this by converting it to a structure and using fieldnames() to get the different fields. A fully loaded object returns a large number of fields, but most times only a fraction is returned.
We need to find a way to loop or wait until the document is loaded and ready before we can proceed with manipulating it. This far, I have not been able to find a suitable solution and any help is wanted.
Br
Fredrik Walka
  댓글 수: 7
Mario Malic
Mario Malic 2023년 8월 25일
One thing you can try is to simply set a pause for several seconds and see if the properties are added after this time. Of course, it's not a great solution but it could reveal if what you are doing is right and document is opening in a corect way.
There is some info here https://forums.autodesk.com/t5/net/how-to-determine-when-a-document-is-completely-loaded/td-p/2684614 about the EndOpen event which could be a way to solve this.
Fredrik Walka
Fredrik Walka 2023년 8월 25일
Hi again,
After some research, I have made some changes to my code.
First, I have replaced requests of data from using ActiveX dot notation to access child objects at different levels. I know set local variable(s) and use get() to ensure I get the COM interface object correct.
Ex.
modelspace = get(obj.cadHandle.ActiveDocument,'ModelSpace');
retval = invoke(modelspace,'AddText',txt,position',textHeight);
obj.WaitForAutoCAD;
%retval = obj.cadHandle.activeDocument.modelSpace.AddText(txt,position',textHeight);
The commented code is the original. These fixes seems to have solved the undefined method/property errors.
I also created a function to wait for AutoCAD to be ready for further commands, since I got the Call was rejected by callee error alot. Adding this after every command manipulating the document seems to work as well. The code for this looks like:
function WaitForAutoCAD(obj)
while 1
try
retval=obj.cadHandle.Application.GetAcadState;
v = retval.IsQuiescent;
if v == 0
pause(1);
else
break;
end
catch err
%disp(err);
% Do nothing, just keep looping
end
end
end

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by