Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

Extract Elevation from Surface by X & Y Coordinates

Satish_Rajdev
Advocate Advocate
780 Views
4 Replies
Message 1 of 5

Extract Elevation from Surface by X & Y Coordinates

Satish_Rajdev
Advocate
Advocate

Hello,

 

I'm trying to write a function to extract elevation from tinsurface from x & y coordinates. I've written following code but it's giving error and I'm not able to solve it. 

        [LispFunction("SURFELEV")]
        public object GETSURFACEELEVATION (ResultBuffer resbuf)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
            TypedValue[] args = resbuf.AsArray();
            TinSurface surf = args[0];
            double PointX = Convert.ToDouble(((TypedValue)args.GetValue(1)).Value);
            double PointY = Convert.ToDouble(((TypedValue)args.GetValue(2)).Value);
            double z = surf.FindElevationAtXY(PointX, PointY);
            return z;
        }

my lisp function will be:

(SurfElev <SurfaceEntity> <XCoord> <YCoord>)

Thanks & Regards,

Satish

Best Regards,
Satish Rajdev


REY Technologies | Linked IN | YouTube Channel


 

0 Likes
Accepted solutions (1)
781 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor
Accepted solution

By saying "it's giving error...", you should have provide more details, such as when the said error raised, and what was the error message, so that others do not have to do the guess game Man Mad .

 

But let me try to guess: the C# code probably does not pass compiling here:

            TypedValue[] args = resbuf.AsArray();
            TinSurface surf = args[0];

because surf is type of TinSurface of Civil3D, while arg[0] is a type of TypedValue. Thus the code will not compile.

 

In your Lisp function call, you did not explicitly say what data type the argument <SurfaceEntity> is, but likely, it is the surface entity's ObjectId. so, the arg[0] as TypedValue should be something like (LispDataType.ObjectId, [id]). Therefore, in your C# code, you should use a Transaction to open the TinSurface with the passed-in ObjectId. better yet, you may want to first make should it is ObjectId. Something like:

 

var args = resbuf.AsArray();

if (args.Length == 3)

{

   if (args[0].TypeCode == ListDataType ) 

   {

       var id=(ObjectId)args[0].Value;

       //Then you can open the Entity with this ObjectId as TinSurface

       // You may also want to test ObjectId.ObjectClass.DxfName to make sure 

       // the entity is TinSurface before you open it in transaction

   }

   else

   {

      // The first piece of data is not an objectId, thus, cannot continue

   }

}

else

{

   //The passed-in argument is not good as expected (must be 3 pieces of data)

}

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

Jeff_M
Consultant
Consultant

Why create a LispFunction for this? You can do this in lisp already, for example:

(setq entpick (entsel "\nSelect a surface: "))
(setq surf (vlax-ename->vla-object (car entpick)))
(setq elev (vlax-invoke surf 'findelevationatxy (car (last entpick)) (cadr (last entpick))))
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 4 of 5

Satish_Rajdev
Advocate
Advocate

 

I've made changes as per your comments and it working beautifully for me now.

 

Thank you so much for great explanation.

 

@Jeff_M 

Thank for the help but I'm already aware of this and using this code from last 2 years but while working on large drawings lisp gets slower and that's why I've written my lisp into c# where it takes few seconds to perform operation.

 

 

Best Regards,
Satish Rajdev


REY Technologies | Linked IN | YouTube Channel


 

0 Likes
Message 5 of 5

LarryGrube
Explorer
Explorer

Jeff,

 

Thanks for that code!  About two decades ago we had a program by Schreiber called Quicksurf which allowed you to create a surface from masspoints and breaklines and you could access the elevations with a simple Quicksurf provided lisp routine.  Quicksurf went out of support about a decade ago.  I had a lisp routine for making a tabulation of the DZ's between photogrammetric C3D topo and field check shots.  The three lines of code you provided allowed me to bring this back to life.

 

Much appreciated.

 

Larry