.NET

Reply
Active Member
hongmei
Posts: 8
Registered: ‎04-18-2012
Message 1 of 13 (789 Views)

Autocad 2013, plane cut solid3d, to get region, access region, autocad crash

789 Views, 12 Replies
04-03-2013 11:56 AM
 
Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 2 of 13 (783 Views)

Re: Autocad 2013, plane cut solid3d, to get region, access region, autocad crash

04-03-2013 12:05 PM in reply to: hongmei

Maybe you post yours code?


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Member
hongmei
Posts: 8
Registered: ‎04-18-2012
Message 3 of 13 (778 Views)

Re: Autocad 2013, plane cut solid3d, to get region, access region, autocad crash

04-03-2013 12:21 PM in reply to: Alexander.Rivilis

using

System;

using

System.Collections.Generic;

using

System.Linq;

using

System.Text;

using

Autodesk.AutoCAD.Runtime;

using

Autodesk.AutoCAD.ApplicationServices;

using

Autodesk.AutoCAD.DatabaseServices;

using

Autodesk.AutoCAD.EditorInput;

using

Autodesk.AutoCAD.Geometry;

using

acadAPP = Autodesk.AutoCAD.ApplicationServices.Application;

namespace

Solid3dCut2013

{

publicclassClass1

{

[

CommandMethod("ACut_Solid3d")]

 

publicstaticvoidACut_Solid3d()

{

Document doc=acadAPP.DocumentManager .MdiActiveDocument ;

 

try

{

ObjectIdsolId;

 

Planepln;

 

ObjectIdCollectionobjIds;

pickSolid3d (doc,

out solId ,out pln ,outobjIds);

 

if (solId ==ObjectId .Null || pln ==null ) return;

 

using (DocumentLockdlock=doc.LockDocument ())

{

using (Transactiontrans=doc.TransactionManager .StartTransaction ())

{

foreach (ObjectId objId inobjIds)

{

CutSolid(doc, trans, objId , pln);

}

trans .Commit ();

}

}

}

catch (System.Exceptionex)

{

doc.Editor .WriteMessage (ex.Message +

"\n");

}

}

publicstaticvoid pickSolid3d(Document doc, outObjectId solId, outPlane pln,outObjectIdCollectionobjIds)

{

solId =

ObjectId.Null;

pln =

null;

objIds =

null;

 

PromptSelectionResultpsr= doc.Editor.SelectAll();

objIds =

newObjectIdCollection( psr.Value.GetObjectIds());

 

PromptEntityOptions peo = newPromptEntityOptions("pick solid3d: ");

peo.SetRejectMessage(

"solid3d only \n");

peo.AddAllowedClass(

typeof(Solid3d), true);

 

PromptEntityResultper = doc.Editor.GetEntity(peo);

 

while (true)

{

if (per.Status == PromptStatus.Cancel) return;

 

if (per.Status != PromptStatus.OK) continue;

solId =per.ObjectId ;

Point3dpt=per.PickedPoint ;

 

using (DocumentLockdlock=doc.LockDocument ())

{

using (Transactiontrans=doc.TransactionManager .StartTransaction ())

{

Solid3d sol=(Solid3d ) trans .GetObject (solId ,OpenMode.ForRead );

 

doublez= (sol.GeometricExtents .MinPoint.Z +sol .GeometricExtents .MaxPoint .Z ) /2;

pt=

newPoint3d(pt.X,pt.Y ,z);

pln=

newPlane (pt,newVector3d(1,1,1).GetNormal () );

trans .Commit ();

}

}

return;

}

}

staticpublicvoid CutSolid(Document doc, Transaction tr, ObjectId solId, Planepln)

{

Entity ent = (Entity)(tr.GetObject(solId, OpenMode.ForRead));

 

if (ent == null)

 

return;

 

if (ent.GetType() != typeof(Solid3d))

 

return;

 

Solid3d sol = (Solid3d)ent;

 

sol.Highlight();

doc.Editor.WriteMessage(

"objId: "+solId .ToString ()+"\n");

 

PromptStringOptions pso = newPromptStringOptions("continu...");

doc.Editor.GetString(pso);

sol.Unhighlight();

 

Region reg = null;

 

try

{

reg = sol.GetSection(pln);

}

catch (Autodesk.AutoCAD.Runtime.Exceptionex)

{

doc.Editor.WriteMessage(ex.Message +

"\n");

 

return;

}

catch (System.Exceptione)

{

//MessageBox.Show(e.Message);

doc.Editor .WriteMessage (e.Message +

"\n");

 

return;

}

try

{

if (reg==null || reg.IsNull) //add region to DB, otherwise 2013 crash return;

}

catch (System.AccessViolationExceptionex)

{

doc.Editor.WriteMessage(ex.Message +

"\n"); // MessageBox.Show(ex.Message);return;

}

catch (System.Exceptionex2)

{

doc.Editor.WriteMessage(ex2.Message +

"\n"); //MessageBox.Show(ex2.Message);

}

DBObjectCollection entCol = newDBObjectCollection();

reg.Explode(entCol);

////add region to DB

BlockTable bt = (BlockTable)(tr.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite));

 

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

btr.AppendEntity(reg);

tr.AddNewlyCreatedDBObject(reg,

true);

reg.Erase();

if (entCol != null&& entCol.Count != 0)

{

foreach (DBObject obj inentCol)

{

if (!(obj isEntity)) continue;

 

Entity ent1 = (Entity)obj;

ent1.ColorIndex = 6;

//pink

btr.AppendEntity(ent1);

tr.AddNewlyCreatedDBObject(obj,

true);

}

}

}

}

}

 

 

For some solid3d (not all),  can pass region=solid.getsection();

but when run to the yellow line, all region's properties can't access, can't catch, just cause autocad 2013 crash, ACAD2011 is ok, only ACAD2013

 

 

Thank you very much

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 4 of 13 (761 Views)

Re: Autocad 2013, plane cut solid3d, to get region, access region, autocad crash

04-03-2013 01:47 PM in reply to: hongmei

1. First of all your code is not readable. Many of keywords are concatenated and if you try copy code from forum to VS and compile it then you get errors. In order to post code please use Insert Code button:

01-03-2013 15-59-28.png

2. I did not see any yellow line although I'm not color blind.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Member
hongmei
Posts: 8
Registered: ‎04-18-2012
Message 5 of 13 (756 Views)

Re: Autocad 2013, plane cut solid3d, to get region, access region, autocad crash

04-03-2013 02:04 PM in reply to: Alexander.Rivilis

pink , sorry, I colored it yellow, then I realized yellow is not bright enough, so I changed it to pink, when I wrote the message, I still think the yellow color

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 6 of 13 (751 Views)

Re: Autocad 2013, plane cut solid3d, to get region, access region, autocad crash

04-03-2013 02:15 PM in reply to: hongmei

Sorry but in any case, I can not read this code ... Maybe someone else ...


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Member
hongmei
Posts: 8
Registered: ‎04-18-2012
Message 7 of 13 (721 Views)

Re: Autocad 2013, plane cut solid3d, to get region, access region, autocad crash

04-04-2013 05:42 AM in reply to: hongmei
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

using acadAPP = Autodesk.AutoCAD.ApplicationServices.Application;
namespace Solid3dCut2013
{
    public class Class1
    {
        [CommandMethod("ACut_Solid3d")]
        public static void ACut_Solid3d()
        {
            Document doc=acadAPP .DocumentManager .MdiActiveDocument ;
            try
            {
                ObjectId solId;
                Plane pln;
                ObjectIdCollection objIds;

                pickSolid3d (doc,out solId ,out pln ,out   objIds);
                if (solId ==ObjectId .Null || pln ==null )  return ;
                using (DocumentLock dlock=doc.LockDocument ())
                {
                    using (Transaction trans=doc.TransactionManager .StartTransaction ())
                    {
                        foreach (ObjectId objId in objIds)
                        {
                            CutSolid(doc, trans, objId , pln);
                        }
                        trans .Commit ();
                    }
                }
            }
            catch (System.Exception ex)
            {
                doc.Editor .WriteMessage (ex.Message +"\n");
            }
        }
        public static void pickSolid3d(Document doc, out ObjectId solId, out Plane pln,out ObjectIdCollection objIds)
        {
            solId = ObjectId.Null;
            pln = null;
            objIds = null;
            
            PromptSelectionResult psr= doc.Editor.SelectAll();
            objIds =new ObjectIdCollection ( psr.Value.GetObjectIds());

            PromptEntityOptions peo = new PromptEntityOptions("pick solid3d: ");
            peo.SetRejectMessage("solid3d only \n");
            peo.AddAllowedClass(typeof(Solid3d), true);
            

            PromptEntityResult per = doc.Editor.GetEntity(peo);
            while (true)
            {
                if (per.Status == PromptStatus.Cancel) return ;
                if (per.Status != PromptStatus.OK) continue;

                solId =per.ObjectId ;
                Point3d pt=per.PickedPoint ;
                using (DocumentLock dlock=doc.LockDocument ())
                {
                    using (Transaction trans=doc.TransactionManager .StartTransaction ())
                    {
                        Solid3d sol=(Solid3d ) trans .GetObject (solId ,OpenMode .ForRead );
                        double z= (sol.GeometricExtents .MinPoint.Z +sol .GeometricExtents .MaxPoint .Z ) /2;
                        pt=new Point3d (pt.X,pt.Y ,z);
                        pln=new Plane (pt,new Vector3d (1,1,1).GetNormal ()  );
                        trans .Commit ();
                    }
                }
                return ;
            }

        }
        static public void CutSolid(Document doc, Transaction tr, ObjectId solId, Plane pln)
        {
            Entity ent = (Entity)(tr.GetObject(solId, OpenMode.ForRead));
            if (ent == null)
                return;
            if (ent.GetType() != typeof(Solid3d))
                return;
            Solid3d sol = (Solid3d)ent;
            
            sol.Highlight();

            doc.Editor.WriteMessage("objId: "+solId .ToString ()+"\n");
            PromptStringOptions pso = new PromptStringOptions("continu...");
            doc.Editor.GetString(pso);
            sol.Unhighlight();


            Region reg = null;
            try
            {
                reg = sol.GetSection(pln);
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                doc.Editor.WriteMessage(ex.Message + "\n");
                return;
            }
            catch (System.Exception e)
            {
                //MessageBox.Show(e.Message);
                doc.Editor .WriteMessage (e.Message +"\n");
                return;
            }
            

            try
            {
                if (reg==null || reg.IsNull)  //add region to DB, otherwise 2013 crash                
                    return;
            }
            catch (System.AccessViolationException ex)
            {
                doc.Editor.WriteMessage(ex.Message + "\n"); // MessageBox.Show(ex.Message);
                
                return;
            }
            catch (System.Exception ex2)
            {
                doc.Editor.WriteMessage(ex2.Message + "\n"); //MessageBox.Show(ex2.Message);
            }

            DBObjectCollection entCol = new DBObjectCollection();
            reg.Explode(entCol);

////add  region to DB
            BlockTable bt = (BlockTable)(tr.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite));
            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
            btr.AppendEntity(reg);
            tr.AddNewlyCreatedDBObject(reg, true);
            reg.Erase();

            
            if (entCol != null && entCol.Count != 0)
            {
                foreach (DBObject obj in entCol)
                {
                    if (!(obj is Entity)) continue;
                    
                    Entity ent1 = (Entity)obj;
                    ent1.ColorIndex = 6;  //pink
                    btr.AppendEntity(ent1);
                    tr.AddNewlyCreatedDBObject(obj, true);                    
                }
            }
        }

    }
}

 

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 8 of 13 (715 Views)

Re: Autocad 2013, plane cut solid3d, to get region, access region, autocad crash

04-04-2013 06:26 AM in reply to: hongmei

I have to your code so many questions that do not even know where to start.

 

1. Function pickSolid3d:

a) if user not select Solid3d (missed) then infinite loop and AutoCAD hang because you do not change per inside the while loop.

b) you really need such cutting plane:

            pln = new Plane(pt, new Vector3d(1, 1, 1).GetNormal());

or you need horizontal cutting plane?

 

2. Function CutSolid:

a) why you open BlockTabe forwrite?

b) what entities you can try add to BlockTableRecord - Region or exploded Region?

c) why you add Region to BlockTableRecord and once it is erased?

 

This is just what I saw quickly viewing your code.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Member
hongmei
Posts: 8
Registered: ‎04-18-2012
Message 9 of 13 (710 Views)

Re: Autocad 2013, plane cut solid3d, to get region, access region, autocad crash

04-04-2013 08:03 AM in reply to: Alexander.Rivilis

Thank you for your patience,

 

I was about to ask ADN support this question, but for some reason, I can't access ADN my account

 

This piece of code is I made up temporary to raise the error, it is full of mistakes if you set a high standard.

my purpose is to make the problem appear, on this line, reg.isNull || reg==null , cause autocad2013 crash, which I couldn't catch

 

your question

1. picksolid3d

  a.my purpose is to pick a solid, I have a drawing which has all solid3d

  b. my cut plane can be any, I just make up this one to make the error appear, horizontal plane can't make the error

 

2. cutsolid

  a.blockTable open for write, not necessary

  b.entities add to model space are entites after explode the region,

  c. region is created tempary, if I don't do anything , it will cause memory leak, if I delete it directly will cause error 'not in database'. so I need to add it to model splace, add to transaction, then I can delete it safely and no memery leak

 

 

Thanks

 

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 10 of 13 (707 Views)

Re: Autocad 2013, plane cut solid3d, to get region, access region, autocad crash

04-04-2013 09:26 AM in reply to: hongmei

hongmei wrote:

...I was about to ask ADN support this question, but for some reason, I can't access ADN my account...


Are you ADN member? If yes why you can not access DevHelp? Maybe you have to e-mail ADN administrator? If you have not his e-mail then PM me.


hongmei wrote:
...my purpose is to make the problem appear, on this line, reg.isNull || reg==null , cause autocad2013 crash, which I couldn't catch

I can reproduce this behavior. Maybe it's a bug, maybe no...  If cutting plane has not intersection with Solid3d then calling reg.isNull produce exception.


hongmei wrote:
...  c. region is created tempary, if I don't do anything , it will cause memory leak, if I delete it directly will cause error 'not in database'. so I need to add it to model splace, add to transaction, then I can delete it safely and no memery leak

As usually enough to call reg.Dispose() in order to free its memory.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Announcements
Autodesk Alert | We are currently working on restoring Previous Version License request access in the Autodesk Subscription Center. If you are a subscription customer and would like to request a Previous Version License, please use this link as a temporary workaround. We apologize for the inconvenience and thank you for your patience.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.