Is there a way to define commands in batches?

Is there a way to define commands in batches?

1039574776
Enthusiast Enthusiast
935 Views
7 Replies
Message 1 of 8

Is there a way to define commands in batches?

1039574776
Enthusiast
Enthusiast

Hello everyone, I have an amazing idea🤣. Is it possible to define commands in batches based on a list of block names. Is this possible? Like the code example below, I know, it doesn't work correctly, so would like to try to get help to implement this idea, thanks in advance.

1.

        [CommandMethod("SetCommands")]
        public static void SetCommands()
        {
            List<string> commandList = new List<string>()
            {
                "CMD1",
                "CMD2",
                "CMD3",
                "CMD4",
                "CMD5",
                "CMD6"
            };
            CommandMethods(commandList);
        }

2.

        public static void CommandMethods(List<string> commandList)
        {
            foreach (string globalName in commandList)
            {
                [CommandMethod(globalName)]
                CreateInsertBlockrefence(globalName);
            }
        }
        public static ObjectId CreateInsertBlockrefence(string blockName)
        {
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            Editor ed=acDoc.Editor;
            Point3d postion = ed.GetPoint("\nSelect a point:").Value;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                
                if (!acBlkTbl.Has(blockName)) return ObjectId.Null; 
                
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                BlockReference acBlk;
                
                ObjectId btrId = acBlkTbl[blockName];
                
                BlockTableRecord record = btrId.GetObject(OpenMode.ForRead) as BlockTableRecord;
                
                acBlk = new BlockReference(postion, acBlkTbl[blockName]);
                acBlkTblRec.AppendEntity(acBlk);
                
                acCurDb.TransactionManager.AddNewlyCreatedDBObject(acBlk, true);
                acTrans.Commit();
                return acBlk.ObjectId;
            }
        }

 

0 Likes
Accepted solutions (3)
936 Views
7 Replies
Replies (7)
Message 2 of 8

norman.yuan
Mentor
Mentor
Accepted solution

One surely can "define" command dynamically with code with this method:

 

Autodesk.AutoCAD.Internal.Utils.AddCommand()

 

in acmgd.dll

 

HTH

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 8

1039574776
Enthusiast
Enthusiast

Thanks for your reply, the Autodesk.AutoCAD.Internal.Utils.AddCommand() method is public static void AddCommand(string cmdGroupName, string cmdGlobalName, string cmdLocalName, CommandFlags cmdFlags, CommandCallback func), the fifth parameter method of the method is just a delegate that takes no arguments and returns void.

public delegate void CommandCallback();

so that the command name I define is still not associated with the block name. Is there any other way to achieve this? Meanwhile I'm trying to find other solutions.

0 Likes
Message 4 of 8

kerry_w_brown
Advisor
Advisor

Does this help ?

 

https://forums.autodesk.com/t5/net/utils-addcommand-function-with-params/td-p/4758899

 

Regards,

Kerry

 


// 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
0 Likes
Message 5 of 8

1039574776
Enthusiast
Enthusiast

Thanks also, I looked up this link and it wasn't what I was looking for. I want to pass parameters into the method, so that the command name is consistent with the block name, that is, the command name is the block name, so that commands are defined in batches according to all block names.

0 Likes
Message 6 of 8

essam-salah
Collaborator
Collaborator
Accepted solution

@1039574776 

have you tried to use Autodesk.AutoCAD.Internal.Utils.GetLastCommandLines() ?

0 Likes
Message 7 of 8

1039574776
Enthusiast
Enthusiast
Accepted solution

@essam-salah @norman.yuan Thanks a lot, after debugging ļ¼Œcombined with Autodesk.AutoCAD.Internal.Utils.AddCommand() and Autodesk.AutoCAD.Internal.Utils.GetLastCommandLines(), exactly what I expected, cheers! This link has its usage example code, hope it will be more helpful to others who need it.

https://forums.autodesk.com/t5/net/catching-command-window-output-with-net-api/m-p/9023171 

0 Likes
Message 8 of 8

essam-salah
Collaborator
Collaborator

@1039574776 wrote:

@essam-salah @norman.yuan Thanks a lot, after debugging ļ¼Œcombined with Autodesk.AutoCAD.Internal.Utils.AddCommand() and Autodesk.AutoCAD.Internal.Utils.GetLastCommandLines(), exactly what I expected, cheers! ..


glad you your problem solved.

0 Likes