Author Topic: How do you start a command?  (Read 3432 times)

0 Members and 1 Guest are viewing this topic.

dugk

  • Guest
How do you start a command?
« on: August 04, 2010, 03:18:53 PM »
I want to mimic this AutoLISP code:  (command"_PSREOPT"ob%view_options) - where ob%view_options is an entity name as delivered by the AutoLISP method (entsel.

I have tried several "things" but none match the functionality of the AutoLISP code above.  Which pauses the current command (a windows form) and runs the command PSREOPT.  I also need to pass it the entity that the current C# command has had the user select.

I've tried:
Code: [Select]
acActiveDoc.SendStringToExecute("_psReOpt", true, false, false);but that doesn't pause AutoCAD for the PSREOPT to run.  It just starts after my current command is done.  It also doesn't have the entity being passed so the user has to select it again.

I've also tried:
Code: [Select]
object[] parameters;
                                    object acadapp = null;
                                    object cmd = null;
                                    object dwg = null;
                                    acadapp = (object)Marshal.GetActiveObject("AutoCAD.Application");
                                    dwg = acadapp.GetType().InvokeMember("ActiveDocument", BindingFlags.GetProperty, null, acadapp, null);
                                    //string sTemp = "(Command " + @"""" + "_PSREOPT" + @"""" + " " + @"""" + "l" + @"""" + " )" + "\r";
                                    string sTemp = "(Command" + @"""" + "_PSREOPT" + @"""" + @"""" + "p" + @"""" + ")" + "\r";
                                    parameters = new object[1];
                                    parameters[0] = sTemp;
                                    cmd = dwg.GetType().InvokeMember("SendCommand", BindingFlags.InvokeMethod, null, dwg, parameters);
but that too doesn't start the command until after the form is closed.  And the PSREOPT command isn't allowing the "_P" (previous) selection option so I'm guessing I need to pass it the entity name like in the AutoLISP code above.  I tried the "_p" selection option from the command line and it wasn't allowed that way either.

I've also looked at this code:  http://www.theswamp.org/index.php?topic=33352.msg387539#msg387539 but it too seems overly complicated.

I also tired Tony T's CommandLine.Command("_PSREOPT", "_p"); and that too runs after the form is closed and the "_p" isn't allowed.

Thank you for any suggestions!
Doug

LE3

  • Guest
Re: How do you start a command?
« Reply #1 on: August 04, 2010, 04:09:09 PM »
don't know, i normally use something like:
Code: [Select]
        private void placeJoistToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetFocus(Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle);
            acadApp.DocumentManager.MdiActiveDocument.SendStringToExecute("PlaceJoist ", true, false, false);
        }

and notice the space in front of the command name "PlaceJoist " <===
hth.

LE3

  • Guest
Re: How do you start a command?
« Reply #2 on: August 04, 2010, 04:13:30 PM »
also.... remember that the pause in the autolisp it is just this: "\\"
i don't use lisp on my c# code, do not know if could work
hth...

dugk

  • Guest
Re: How do you start a command?
« Reply #3 on: August 04, 2010, 05:31:29 PM »
I tried your suggestion but I didn't work for me.  I think it is more than likely the do/while loop I have it in.

I'll try to figure out a new structure for the code and see if I can get your suggestion to work.

But I'm open to all other suggestions.

Thanks!

Jerry J

  • Newt
  • Posts: 48
Re: How do you start a command?
« Reply #4 on: August 04, 2010, 08:35:18 PM »
I think you need to make sure that the Ename you have is in String format. I would construct the whole command string.  I don't know if you can really pass an object with the executestring method though.

LE3

  • Guest
Re: How do you start a command?
« Reply #5 on: August 04, 2010, 09:09:37 PM »
don't have time to run any tests, but here is other stuff that I use, see if helps:
Code: [Select]
public static PromptStatus RunCmd(Editor ed, params object[] args)
        {
            System.Type t = ed.GetType();
            PromptStatus res = (PromptStatus)t.InvokeMember("RunCommand", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, ed, args);
            return res;
        }

some samples usage:

            PromptSelectionResult res = null;
            res = ed.GetSelection(options, filter);

            if (res.Status == PromptStatus.OK && res.Value.Count > 0)
            {                
                object o_cmdecho = AcadApp.GetSystemVariable("CMDECHO");
                AcadApp.SetSystemVariable("CMDECHO", (object)0);

                PromptStatus PS = RunCmd(ed, "DimEdit", "Oblique", res.Value, "", "135");

                if (PromptStatus.OK == PS || PromptStatus.Cancel == PS)
                    AcadApp.SetSystemVariable("CMDECHO", o_cmdecho);
            }
Code: [Select]
PromptStatus PS = Functions.RunCmd(ed, "._ZOOM", "_O", psr.Value, "", "");
Code: [Select]
object o_cmdecho = AcadApp.GetSystemVariable("CMDECHO");
                    AcadApp.SetSystemVariable("CMDECHO", (object)0);

                    PromptStatus PS;

                    PS = Functions.RunCmd(doc.Editor, "._MVIEW", "_O", ent.ObjectId);
                    
                    doc.Editor.SwitchToModelSpace();

                    PS = Functions.RunCmd(doc.Editor, "._-VIEW", "_R", spoolName);

                    if (PromptStatus.OK == PS || PromptStatus.Cancel == PS)
                        AcadApp.SetSystemVariable("CMDECHO", o_cmdecho);

I tried your suggestion but I didn't work for me.  I think it is more than likely the do/while loop I have it in.

I'll try to figure out a new structure for the code and see if I can get your suggestion to work.

But I'm open to all other suggestions.

Thanks!
« Last Edit: August 04, 2010, 09:15:51 PM by LE »

dugk

  • Guest
Re: How do you start a command?
« Reply #6 on: August 05, 2010, 12:25:44 AM »
Dude!

It is late and I'm off to bed but I tried your code and it worked!!!

THANK YOU!

I'll confirm tomorrow morning and update as necessary.

Thanks!
Doug

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How do you start a command?
« Reply #7 on: August 05, 2010, 01:22:21 AM »
Afternoon Luis

Just a question ..
why do you cast the value parameter in .SetSystemVariable to object

ie
AcadApp.SetSystemVariable("CMDECHO", (object)0);


I may be misunderstand the compiler process but I don't believe it is needed as I think the compiler looks after it

I know that the signature calls for an object, but ...

with this code c#
Code: [Select]
       [CommandMethod("tset")]
        public void testset ()
        {
            AcadApp.SetSystemVariable("CMDECHO", (object)0);
            AcadApp.SetSystemVariable("CMDECHO", 1);
        }


The generated dll through Reflection in c# is
 
Code: [Select]

[CommandMethod("tset")]
public void testset()
{
    Application.SetSystemVariable("CMDECHO", 0);
    Application.SetSystemVariable("CMDECHO", 1);
}

 

and reflection into CIL is
Code: [Select]
.method public hidebysig instance void testset() cil managed
{
    .custom instance void [acmgd]Autodesk.AutoCAD.Runtime.CommandMethodAttribute::.ctor(string) = { string('tset') }
    .maxstack 8
    L_0000: nop

    L_0001: ldstr "CMDECHO"
    L_0006: ldc.i4.0
    L_0007: box int32
    L_000c: call void [acmgd]Autodesk.AutoCAD.ApplicationServices.Application::SetSystemVariable(string, object)
    L_0011: nop

    L_0012: ldstr "CMDECHO"
    L_0017: ldc.i4.1
    L_0018: box int32
    L_001d: call void [acmgd]Autodesk.AutoCAD.ApplicationServices.Application::SetSystemVariable(string, object)
    L_0022: nop

    L_0023: ret
}


so by my reckoning both statements are equivalent.

[added]
Though I s'pose showing the casting is an indication of intent and a transparent convention that may be less confusing for casual viewers.

[added later]
but then again .. most of us should be familiar with seeing
(setvar "CMDECHO" 0)

:)
« Last Edit: August 05, 2010, 01:55:54 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE3

  • Guest
Re: How do you start a command?
« Reply #8 on: August 05, 2010, 09:31:26 AM »
Good.

I'll confirm tomorrow morning and update as necessary.
« Last Edit: August 05, 2010, 10:24:09 AM by LE »

LE3

  • Guest
Re: How do you start a command?
« Reply #9 on: August 05, 2010, 09:40:11 AM »
Hi Kerry,
I have to document my code that way, since it is going to be use by some other programmers, and it is a requirement :).

why do you cast the value parameter in .SetSystemVariable to object
edit: taking out all the quoted comments from the previous post
« Last Edit: August 06, 2010, 10:37:51 PM by LE »