How to wait for a command to execute

How to wait for a command to execute

SaddamShaikh77
Advocate Advocate
2,898 Views
6 Replies
Message 1 of 7

How to wait for a command to execute

SaddamShaikh77
Advocate
Advocate
Hello,

I am trying to execute the command "doc.SendStringToExecute((string)button.CommandParameter + "PDFATTACH", true, false, true);" or "doc.SendStringToExecute((string)button.CommandParameter + "PDFIMPORT", true, false, true);"

// some code after this

I want to execute the above lines completely and the next code should execute. Now it is not working as expected. I tried thread.sleep, task, async-await, but couldn't work for me.

 

Document doc = aCAD_App.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
"ed.CommandAsync("PDFATTACH", true, false, true);"

This also not worked.(I am trying to attach pdf in empty document)

also, 
Please suggest me any solution for this

0 Likes
2,899 Views
6 Replies
Replies (6)
Message 2 of 7

norman.yuan
Mentor
Mentor

In general, you can handle Document.CommandEnded event, and have your extra code to be executed if the finished command is the targeted command.

 

Among the 2 command you want to execute by SendStringToExecute() or Editor.Command[Async](), "PDFATTACH" does have the command line version, and you need to call it "-PDFATTACH" to avoid its dialog box pops up, and you need to supply all its needed inputs. However, "PDFIMPORT" does not have command line version, there is no way to supply the required inputs in either SendStringToExecute() or Editor.Command(), so I do not think you can do it with "PDFIMPORT".

 

Also, while it is possible to call SendStringToExecute() with command "-PDFATTACH", and handle the CommandEnded event to let your code go further, it would be much better to write your own API code to attach Pdf underlay, and do extra work after Pdf underlay being attached, just similar to you do with inserting block into drawing.

 

One of my article discusses how to insert multi-paged Pdf as underlay. Some of the code there shows how to insert Pdf underlay:

 

https://drive-cad-with-code.blogspot.com/2019/06/attach-pdf-underlay-how-to-handle.html 

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 7

SaddamShaikh77
Advocate
Advocate

Thank you, Norman.
The event handler worked for me. Now I can select pages and attached them in an empty drawing.
Next, I want to read all underlays in the current drawing and to convert all underlay to block references using "PDFIMPORT". I want to read the geometry from the pdf pages.
Could you please guide me how to achieve this.

0 Likes
Message 4 of 7

norman.yuan
Mentor
Mentor

The process of converting geometry shapes in PDF file into AutoCAD native entities behind the command "PDFIMPORT" is not exposed in .NET API. So, there is no easy way to do that other than run the command "PDFIMPORT" command manually.

 

Besides AutoCAD's "PDFIMPORT" command, there are other third part tools also doing that work, but I am not sure whether there are API libraries available or not. If you really want to re-invent your own wheel, you can study PDF document (it is open file format), or find a suitable PDF manipulating libraries to collect shapes' geometric information and do your own AutoCAD native entity conversion. It would not be easy task, to say the least. I'd rather wait and remotely hope one day Autodesk makes the process PDF importing (as AutoCAD entities) into API, knowing it could never be available in API🙄.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 7

SaddamShaikh77
Advocate
Advocate

Hello Noman,
As discussed earlier, I have used commandended event but now it is failing

step1. ImportDwgTemplate(acDocMgr, ref doc);
//doc.CommandEnded += new CommandEventHandler(CommandEndedEvent);
step2. doc.SendStringToExecute("PDFIMPORT \n", true, false, true);
step3. doc.SendStringToExecute("LOADTRUSS \n", false, false, true);

In above code, After execution of step 1, step 3 is executing and then step 2

I have tried  doc.CommandEnded += new CommandEventHandler(CommandEndedEvent); but still it is not working as expected. 

Please guide me how to achieve this

0 Likes
Message 6 of 7

norman.yuan
Mentor
Mentor

As I previously said, I do not see you can run command "PDFIMPORT" without user interacting with the dialog box that command brings up. But if you can live with that, ...

 

You need to show your code where step2 and 3 is executed. I suppose both are in CommandEnded eventn handler, where your code would test which command is just ended, and you only send command "PDFImport" and/or "LoadTruss" when it is their turn to run. For example, your code only call SendStringToExecute("LOADTRAUSS"....) after "PDFIMPORT" is ended (i.e. your code should make it impossible for step 3 to run before step 2).

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 7

SaddamShaikh77
Advocate
Advocate

Hello, I am using "PDFIMPORT" with user interface only.

Steps are:

1. Load dwg template

2. Import pdf onto it using UI

3. Get other details

The code is given below

 

public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{

//This is button call, executes on click of button
Document doc = aCAD_App.DocumentManager.MdiActiveDocument;
DocumentCollection acDocMgr = aCAD_App.DocumentManager;
ImportPdf(parameter);
}

private void ImportPdf(object parameter)/*, Document doc)*/
{
Document doc = aCAD_App.DocumentManager.MdiActiveDocument;
DocumentCollection acDocMgr = aCAD_App.DocumentManager;
Editor ed = doc.Editor;
if (parameter is RibbonCommandItem button && (button.Id == "Truss_Export"))
{
ImportDwgTemplate(acDocMgr, ref doc);
doc.CommandEnded += new CommandEventHandler(CommandEndedEvent);
doc.SendStringToExecute("PDFIMPORT \n", true, false, true);
//doc.SendStringToExecute("LOADTRUSS \n", false, false, true);
}

}
private void CommandEndedEvent(object sender, CommandEventArgs e)
{
Document doc = aCAD_App.DocumentManager.MdiActiveDocument;
doc.SendStringToExecute("LOADTRUSS \n", false, false, true);
}

 

[CommandMethod("LOADTRUSS", CommandFlags.Session)]
public void GetTrussEntity()
{

FilterSelectionSet();
//GetHatch();
}

I want executions as mentioned in above steps. But now the sequence is step1, step3,step2 and again step3.

Please help me if you know better way.

0 Likes