.NET

Reply
Valued Contributor
HelloWorlddd
Posts: 87
Registered: ‎05-03-2013
Message 1 of 3 (327 Views)
Accepted Solution

How to creat group in AutoCAD with C#?

327 Views, 2 Replies
12-21-2013 02:07 PM

      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
            }
        }

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-using-net.html

 

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 2 of 3 (319 Views)

Re: How to creat group in AutoCAD with C#?

12-21-2013 03:35 PM 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...

 

Valued Contributor
HelloWorlddd
Posts: 87
Registered: ‎05-03-2013
Message 3 of 3 (281 Views)

Re: How to creat group in AutoCAD with C#?

12-22-2013 01:00 AM in reply to: mzakiralam

    Very Thanks, Problem solved

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.