EXPLODE command by C#

EXPLODE command by C#

Anonymous
Not applicable
1,377 Views
3 Replies
Message 1 of 4

EXPLODE command by C#

Anonymous
Not applicable

I have a below code that i have wrote based on the copy command ho i performed.

Please find the below code, Its working until the "getSelection" After that this code it not running.

Could anyone have a good suggestion for this would be appreciated. Please correct me where i am getting wrong.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System;
 
namespace AutoCAD_Libruary
{
    public class Class1
    {
        [CommandMethod("XPLD", CommandFlags.UsePickSet)]
        public void Commands()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction transXPLODE = db.TransactionManager.StartTransaction())
            {
                try
                {
                    BlockTable btXPLODE;
                    btXPLODE = transXPLODE.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord btrXPLODE;
                    btrXPLODE = transXPLODE.GetObject(btXPLODE[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                    object tileMode = Application.GetSystemVariable("TILEMODE");
                    if (System.Convert.ToInt16(tileMode) == 0)

                    {
                        Application.SetSystemVariable("TILEMODE", 1);
                    }

                    PromptSelectionResult psrXplode = ed.GetSelection(); /*SelectWindow(new Point3d(113000, 13000, 0), new Point3d(100000, -500, 0));*/
                    if (psrXplode.Status == PromptStatus.OK)
                    {
                        SelectionSet ssXplode = psrXplode.Value;
                        foreach (SelectedObject sObjXplode in ssXplode)
                        {
                            if (sObjXplode != null)
                            {
                                DBObjectCollection dbOBJcolXplode = new DBObjectCollection();
                                if (dbOBJcolXplode != null)
                                {
                                    foreach (Entity entXplode1 in dbOBJcolXplode)
                                    {
                                        Entity entXplode = transXPLODE.GetObject(sObjXplode.ObjectId, OpenMode.ForRead) as Entity;
                                        entXplode.Explode(dbOBJcolXplode);

                                        if (entXplode != null)
                                        {
                                            foreach (DBObject objDB in dbOBJcolXplode)
                                            {
                                                Entity entXplodeAdd = objDB as Entity;
                                                btrXPLODE.AppendEntity(entXplodeAdd);
                                                transXPLODE.AddNewlyCreatedDBObject(entXplodeAdd, true);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage("Error" + ex.Message);
                    transXPLODE.Abort();
                }
                transXPLODE.Commit();
            }
        }
    }
}
1,378 Views
3 Replies
Replies (3)
Message 2 of 4

lena.talkhina
Alumni
Alumni

Hello @Anonymous !

 

Great to see you on .NET forum. To get more answers on a question you posted here, don't hesitate to post updates so community members will be notified that you still looking for  a solution.

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



Лена Талхина/Lena Talkhina
Менеджер Сообщества - Русский/Community Manager - Russian

Message 3 of 4

fsztuczny
Advocate
Advocate

 Hello

I've patched this code a bit, but it's not for production purposes. Treat it as bug fixes that prevent the launch. You still have to learn a lot to get it working properly.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System;

namespace AutoCAD_Libruary
{
	public class Class1
	{
		[CommandMethod( "XPLD", CommandFlags.UsePickSet )]
		public void Commands()
		{
			Document doc = Application.DocumentManager.MdiActiveDocument;
			Database db = doc.Database;
			Editor ed = doc.Editor;

			using( Transaction transXPLODE = db.TransactionManager.StartTransaction() )
			{
				try
				{
					BlockTable btXPLODE;
					btXPLODE = transXPLODE.GetObject( db.BlockTableId, OpenMode.ForRead ) as BlockTable;
					BlockTableRecord btrXPLODE;
					btrXPLODE = transXPLODE.GetObject( btXPLODE[BlockTableRecord.ModelSpace], OpenMode.ForWrite ) as BlockTableRecord;

					object tileMode = Application.GetSystemVariable( "TILEMODE" );
					if( System.Convert.ToInt16( tileMode ) == 0 )

					{
						Application.SetSystemVariable( "TILEMODE", 1 );
					}

					PromptSelectionResult psrXplode = ed.GetSelection(); /*SelectWindow(new Point3d(113000, 13000, 0), new Point3d(100000, -500, 0));*/
					if( psrXplode.Status == PromptStatus.OK )
					{
						ObjectId[] entColl = psrXplode.Value.GetObjectIds();
						foreach( ObjectId entId in entColl )
						{
							Entity entXplode = transXPLODE.GetObject( entId, OpenMode.ForRead ) as Entity;
							using( DBObjectCollection expObjColl = new DBObjectCollection() )
							{
								entXplode.Explode( expObjColl );

								foreach( DBObject objDB in expObjColl )
								{
									Entity entXplodeAdd = objDB as Entity;
									btrXPLODE.AppendEntity( entXplodeAdd );
									transXPLODE.AddNewlyCreatedDBObject( entXplodeAdd, true );
								}
								if( expObjColl.Count > 0 )
								{
									entXplode.UpgradeOpen();
									entXplode.Erase( true );
								}
							}
						}
					}
					transXPLODE.Commit();
				}
				catch( System.Exception ex )
				{
					ed.WriteMessage( "Error" + ex.Message );
					transXPLODE.Abort();
				}
			}
		}
	}
}

 

0 Likes
Message 4 of 4

Anonymous
Not applicable

Thanks for the code correction,

I will check and let you know the status..!!

 

Thanks

0 Likes