Synchronously Send (and wait for) commands in AutoCAD using ObjectARX

<?xml encoding=”UTF-8″>By Fenton Webb

Leading on from this post, I thought you guys should at least know how to do the same thing in ObjectARX…

Issue

How can I permit a user to draw a multi-point polyline from ObjectARX? I tried to use the acedCommand to send the ‘_pline’ command to AutoCAD, but it’s impossible to predetermine how many polyline points a user will decide to enter.

Solution

A function can be implemented that first issues the PLINE command and then continually checks if PLINE is still active in the AutoCAD command buffer. If so, command pauses are sent to the command line to allow further user input.
This activity can happen repetitively until the user escapes or presses the Enter key to complete the polyline.
The following code segment does this:

<br>Adesk::Boolean isPlineActive() <br>{  <br>    struct resbuf rb;  <br>    acedGetVar(_T("CMDNAMES"),&rb);<br>    if(rb.restype == RTSTR && rb.resval.rstring != NULL)  <br>    {   <br>        //"PLINE" contained in CMDNAMES?<br>        if(_tcsstr(rb.resval.rstring,_T("PLINE"))) <br>            return Adesk::kTrue;  <br>    }  <br>    return Adesk::kFalse;  <br>} 
<p>void mkline() <br>{<br>    acedCommand(RTSTR,_T("_.pline"),RTSTR,PAUSE,RTNONE);  <br>    while(isPlineActive())<br>        acedCommand(RTSTR,PAUSE,RTNONE);   <br>    acutPrintf (_T("nContinue processing")); <br>}</p>

Comments

3 responses to “Synchronously Send (and wait for) commands in AutoCAD using ObjectARX”

  1. Hi Fenton
    Thanks for the good article
    Btw, I’m using acedPostCommand this way instead
    and it works like a charm:
    [System.Security.SuppressUnmanagedCodeSecurity]
    [DllImport(“acad.exe”, CharSet = CharSet.Auto,
    CallingConvention = CallingConvention.Cdecl,
    EntryPoint = “?acedPostCommand@@YAHPB_W@Z”)]
    extern static private int acedPostCommand(string strExpr);
    [CommandMethod(“pipp”)]
    public void tesPlineCommand()
    {
    acedPostCommand(“_.PLINE “);
    bool quit = false;
    // loop round while the PLINE command is active
    while (!quit)
    {
    // see what commands are active
    string cmdNames = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable(“CMDNAMES”);
    // if the PLINE command is active
    if (cmdNames.ToUpper().IndexOf(“PLINE”) >= 0)
    {
    //// then send a PAUSE to the command line
    acedPostCommand(“\”);
    }
    else
    // otherwise quit
    quit = true;
    }
    }
    Kind regards,
    Oleg

  2. Oleg, your code is very dangerous. You should never make any assumptions about when an asynchronous command produces side effects.

  3. Thanks mr.Owen Wengerd,
    You are quite right, if the command runs in the middle of process, then the process stops, but when the code used in the end of
    then everything works without problems
    Kind regards,
    Oleg

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading