.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to creat group in AutoCAD with C#?

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
HelloWorlddd
3281 Views, 2 Replies

How to creat group in AutoCAD with C#?

      I want to creat a group for the object be selected. but the following code doesn't work, what's the problem?
Is there anyone can help me?  very thanks

 


     [CommandMethod("gr")]
        public static void SelectObjectsOnscreen()
        {
            //Get the current document and database
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
    
            //Instance a group object
            Group myGroup = new Group("xxxx", true);

 

            //Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //Request for objects to be selected in the drawing area
                PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();

                //If the prompt status is OK, objects were selected
                if (acSSPrompt.Status == PromptStatus.OK)
                {
                    SelectionSet acSSet = acSSPrompt.Value;

                    //Step through the objects in the selection set
                    foreach (SelectedObject acSSObj in acSSet)
                    {
                        //Check to make sure a valid SelectedObject object was returned
                        if (acSSObj != null)
                        {
                             //Open the selected object for write
                            Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,
                                                             OpenMode.ForWrite) as Entity;
                            if (acEnt != null)
                            {
                                Application.ShowAlertDialog(acEnt.ObjectId.ToString());

 

                                //Use the Append method
                                myGroup.Append(acEnt.ObjectId);
                            }
                        }
                    }
                    //Save the new object to the database
                    acTrans.Commit();
                }
                // Dispose of the transaction
            }
        }

2 REPLIES 2
Message 2 of 3
mzakiralam
in reply to: HelloWorlddd

Hi,

 

I think there are couple of  issues in your code. I have skimmed through you code and it seems to me that   you did not declare the group dictionary. Second, you did not add the new group to group dictionary. Please revise your code. you can find something useful in below link.

 

http://through-the-interface.typepad.com/through_the_interface/2010/01/creating-an-autocad-group-usi...

 

Message 3 of 3
HelloWorlddd
in reply to: mzakiralam

    Very Thanks, Problem solved

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost