LispFunction

LispFunction

bahman.jf.68
Advocate Advocate
680 Views
7 Replies
Message 1 of 8

LispFunction

bahman.jf.68
Advocate
Advocate

Hello everyone,

I have a problem with passing a polyline to C# .Net.

I wrote .Net codes to get an object from Lisp and then calculating Area and also get bounding box of the object, but Autocad crashs after selecting a closed polyline. (I will appreciate anyone who corrects codes and adding bounding box method to it)

 

 

(setq ent (car (entsel)))
(mylispfunc ent)
 [LispFunction("MyLispFunc")]
        public ResultBuffer mylispfunc(ResultBuffer resBuf)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            ResultBuffer rbfResult = default(ResultBuffer);
            var args = resBuf.AsArray();
            ObjectId co = (ObjectId)((TypedValue)(args.GetValue(0))).Value;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Polyline lwp = tr.GetObject(co, OpenMode.ForRead) as Polyline;
                double arr = lwp.Area;
                tr.Commit();
            }

 

 

0 Likes
Accepted solutions (1)
681 Views
7 Replies
Replies (7)
Message 2 of 8

ActivistInvestor
Mentor
Mentor

Have you tried stepping through your code in the debugger to find out what is going on?

 

Also, crash is a vague description. What exactly does AutoCAD do? Does it show an error of some kind? Let this not be a guessing game.

0 Likes
Message 3 of 8

bahman.jf.68
Advocate
Advocate

Yes, the error is to this line :

  double arr = lwp.Area;
0 Likes
Message 4 of 8

norman.yuan
Mentor
Mentor
Accepted solution

Since you have this line of code:

 

Polyline lwp = tr.GetObject(co, OpenMode.ForRead) as Polyline;

 

So, if the opened DBObject by the Transaction is not a Polyline, the variable "lwp" would be points to null, thus the next line will raise error.

 

You should test like this:

Polyline lwp=tr.GetObject(co, OpenMode.ForRead) as Polyline;

if (lwp != null)

{

  double arr = lwp.Area;

}

else

{

   // you may want to raise/throw an invalid argumentException to indicate the selected entity is not a Polyline, for the    calling lisp code to handle.

}

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 8

ActivistInvestor
Mentor
Mentor

You do not mention what specific error it is. If as suggested by another reply the object is not a polyline then it will be a null reference exception. If the object is a polyline then it could be something else.

 

My standard advice on polyline area has always been to use COM to get the area because the managed Curve.Area property can produce incorrect results with closed, self-intersecting polylines.

Message 6 of 8

BlackBox_
Advisor
Advisor

@norman.yuan -

 

Why instantiate the overhead of a Transaction to test for the correct entity type?

 

Wouldn't it be more efficient to instead simply test the ObjectId for co.ObjectClass.Name first, if it passes, then use the Transaction?


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 7 of 8

norman.yuan
Mentor
Mentor

@BlackBox_ : I simply pointed out the direct reason of the error OP gets.

 

If the LISP function does required Polyline, not other curves (Arc, Circle...) as input, then I am with you: I'd first test ObjectId.ObjectClass.Name/ObjectId.ObjectClass.DxfName to make sure it is Polyline before starting a Transaction

Norman Yuan

Drive CAD With Code

EESignature

Message 8 of 8

bahman.jf.68
Advocate
Advocate

Thank you everyone for replying.

The problem resolved with this line : 

 

if (lwp != null)

{

  double arr = lwp.Area;

}

 

@ActivistInvestor Thanks for your advice, I dont have more exprience with COM programming, I'm good at AutoLisp and I have been working with C# Pure .Net for about one year. Also I think Pure .Net is so quicker than Com or Lisp.

But regarding intersection errors, I think all Lisp, vba, C# returns incorrect result. 

0 Likes