Override default commands

Override default commands

annse
Enthusiast Enthusiast
2,863 Views
17 Replies
Message 1 of 18

Override default commands

annse
Enthusiast
Enthusiast

Hi!

 

I'm trying to do something like this:

 

[Autodesk.AutoCAD.Runtime.CommandMethod("REFEDIT", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)]
public void RefEdit()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;

 

if (doc is a document I care about))
{
do my stuff...
}
else
{

run the default command...
}
}

 

I haven't undefined the default command, my definition of REFEDIT executes anyway.

I wanna be able to run the default command from my definition.

 

doc.SendStringToExecute(".REFEDIT ", true, false, false); will loop forever.

 

0 Likes
Accepted solutions (1)
2,864 Views
17 Replies
Replies (17)
Message 2 of 18

Balaji_Ram
Alumni
Alumni

Hi,

 

I tried this which is very similar to what you are trying and it worked ok.

With this code, the line command is redefined only for drawings that have "Test" in their name.

 

Can you try this and see if it works for you ?

Maybe it will then help you identify the reason for your code to loop indefinitely.

 

// Add reference to Autodesk.AutoCAD.Interop 
// Add reference to Autodesk.AutoCAD.Interop.Common
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

[CommandMethod("LINE")]
static public void LineMethod()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    if (doc.Name.Contains("Test"))
        ed.WriteMessage("My implementation of line");
    else
    {
        AcadApplication app = (AcadApplication)Application.AcadApplication;
        app.ActiveDocument.SendCommand("_.LINE ");
        ed.WriteMessage("My implementation of line");
    }
}
    
void IExtensionApplication.Initialize()
{
    AcadApplication app = (AcadApplication)Application.AcadApplication;
    app.ActiveDocument.SendCommand("_.UNDEFINE _LINE ");
}

 

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 18

annse
Enthusiast
Enthusiast

Thank you for your quick response!

 

I've added required references and I now use SendCommand instead of SendStringToExecute.

This will still loop indefinitely. (If doc.Name doesn't contain "Test")

 

public void Initialize()
{

AcadApplication app = (AcadApplication)Application.AcadApplication;
app.ActiveDocument.SendCommand("_.UNDEFINE _REFEDIT ");
}

 

[Autodesk.AutoCAD.Runtime.CommandMethod("REFEDIT", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)]
static public void RefEdit()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;

 

if (doc.Name.Contains("Test"))
{
ed.WriteMessage("New refedit...");
}
else
{
AcadApplication app = (AcadApplication)Application.AcadApplication;
app.ActiveDocument.SendCommand("_.REFEDIT ");
}
}

 

Thanks again!

0 Likes
Message 4 of 18

Balaji_Ram
Alumni
Alumni

Hi,

 

With the Line command the code does work.

 

But there seems to be some problem with using it for "Refedit".

Can you check if the command is actually undefined after sending the "undefine" command ?

 

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 18

Alexander.Rivilis
Mentor
Mentor

What about:

app.ActiveDocument.SendCommand("_ACAD_REFEDIT.REFEDIT ");

 ?

Command _REFEDIT defined in file AcRefEd.arx

If your's dll-file loaded after AcRefEd.arx - than no need to _UNDEFINE _REFEDIT, because your command REFEDIT override standard command REFEDIT. And standard command REFEDIT can be invoked using _ACAD_REFEDIT.REFEDIT

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 6 of 18

annse
Enthusiast
Enthusiast

Hi!

 

When I do the same thing with LINE instead of REFEDIT, my new definition of LINE won't run.

I get this error with both LINE and REFEDIT.

 

_.UNDEFINE
; error: no function definition: ACET-STR-REPLACE
_LINE

 

Thanks!

0 Likes
Message 7 of 18

annse
Enthusiast
Enthusiast

Thanks, Alexander! That works!

 

My dll is loaded in a .bundle installation.

So, because I can't override _LINE the same way, _LINE is loaded after my dll and _REFEDIT before?

 

Thanks again!

0 Likes
Message 8 of 18

annse
Enthusiast
Enthusiast

If the timing issue is irrelevant due to that the _REFEDIT command is already defined when my dll is loading,

I can use SendStringToExecute instead of SendCommand?

This way I won't have to echo the command.

 

Thanks again!

0 Likes
Message 9 of 18

Alexander.Rivilis
Mentor
Mentor

@annse wrote:

I can use SendStringToExecute instead of SendCommand?

This way I won't have to echo the command.


I think you can do it. Try!  🙂

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 10 of 18

annse
Enthusiast
Enthusiast

Hello again...

 

Sending "_ACAD_REFEDIT.REFEDIT " seems to redefine the command.

If the string is executed, the next time you run REFEDIT, it'll run the default definition. 

 

[Autodesk.AutoCAD.Runtime.CommandMethod("REFEDIT", Autodesk.AutoCAD.Runtime.CommandFlags.Session & Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)]
static public void RefEditDef()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;

 

if (doc.Name.Contains("Test"))
{

ed.WriteMessage("selected ref..");

}
else
{
doc.SendStringToExecute("_ACAD_REFEDIT.REFEDIT ", true, false, false);
}
}

 

Any ideas?

 

Thanks!

0 Likes
Message 11 of 18

Alexander.Rivilis
Mentor
Mentor
The reason is that, in fact, AutoCAD arx-application, that handles command _REFEDIT (ie AcRefEd.arx) is loaded when you first run the command and overrides your command REFEDIT. Therefore, need to load AcRefEd.arx before your's dll-file will be loaded:
Autodesk.AutoCAD.Runtime.SystemObjects.DynamicLinker.LoadModule ("AcRefEd.arx", false,true);

 Also you can modify registry to AcRefEd.arx will loaded with AutoCAD startup:

 

HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD\RXX.X\ACAD-AYYY:ZZZ\Applications\AcadRefEdit
LOADCTRLS = 0x02 (instead of 0x0d)

 



Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 12 of 18

annse
Enthusiast
Enthusiast

I've modified the registry. Even if I undefine _REFEDIT, my definition doesn't get called.

0 Likes
Message 13 of 18

Alexander.Rivilis
Mentor
Mentor
Accepted solution

This code was tested with AutoCAD 2011-2013:

 

using System;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

 

[assembly: ExtensionApplication(typeof(Rivilis.RefEditSubst))]

[assembly: CommandClass(typeof(Rivilis.RefEditSubst))]

 

namespace Rivilis

{

    public class RefEditSubst : IExtensionApplication

    {

        void IExtensionApplication.Initialize()

        {

            // Preloading AutoCAD RefEdit command arx-file

            SystemObjects.DynamicLinker.LoadModule("AcRefEd.arx", false, false);

        }

        void IExtensionApplication.Terminate()

        {

            // Do plug-in application clean up here

        }

        //------------------------------------------------

        //     Command group MUST be "ACAD_MAIN"

        //------------------------------------------------

        [CommandMethod("ACAD_MAIN", "RefEdit", CommandFlags.UsePickSet)]

        public void RefEdit() // This method can have any name

        {

            Document doc = Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            ed.WriteMessage("MyRefEdit..");

            if (doc.Name.Contains("Test"))

            {

                ed.WriteMessage("selected ref..");

            }

            else

            {

                doc.SendStringToExecute("_ACAD_REFEDIT.REFEDIT ", true, false, false);

            }

        }

 

    }

}

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 14 of 18

kerry_w_brown
Advisor
Advisor

 

Very nice solution Alexander !!

Regards

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
Message 15 of 18

Alexander.Rivilis
Mentor
Mentor

Thanks! Smiley Happy

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 16 of 18

annse
Enthusiast
Enthusiast

Thank you, Alexander! Setting the command group did it.

0 Likes
Message 17 of 18

Alexander.Rivilis
Mentor
Mentor

@annse wrote:

Thank you, Alexander! Setting the command group did it.


What about accept solution: http://forums.autodesk.com/t5/FAQ-How-To-Using-Autodesk/Accepted-Solutions/td-p/2701533 ?

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 18 of 18

annse
Enthusiast
Enthusiast

Accepted. 🙂

 

Thanks again!

0 Likes