Active Member
Posts: 7
Registered: ‎02-28-2006
Message 1 of 1 (154 Views)

Get Entity properties

154 Views, 0 Replies
03-02-2006 01:42 AM

on .net form button is there.
when i click the button i want to get entity from autocad to .net

I call the test method in Button_Click event.

when i click the button
then it gives the error. Object reference not set to instanitate an object. (runtime error) there are no compliation errors.

on NETLOAD test method is working fine.

when i click the button
at this line it is giving the error.
Database Db = HostApplicationServices.WorkingDatabase;
it is not recognize the classes Editor,Database.

the code is below.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using SampleClasses;
using System.Runtime.InteropServices;
using Microsoft;
using Microsoft.VisualBasic;

using Autodesk.AutoCAD;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using acadApp =
using AcadDB = Autodesk.AutoCAD.DatabaseServices;
using AcadRun = Autodesk.AutoCAD.Runtime;
private void button2_Click(object sender, System.EventArgs e)

public test()
object G_OBJ = null;
ObjectId G_OBJID;
Entity G_ENT = null;
Line G_LINE = null;
string G_ENTTYPE = "", s = "", es = "";

// Here I am Getting the ERROR --> System.NullReferense Error Additional Info: Object Referense not set to an instance of object
Database Db = HostApplicationServices.WorkingDatabase;

Editor Ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Transaction Trans = Db.TransactionManager.StartTransaction();
BlockTable bt = (BlockTable)Trans.GetObject(Db.BlockTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite);
BlockTableRecord btr = (BlockTableRecord)Trans.GetObject(bt[BlockTableRecord.ModelSpace],Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite);
PromptEntityResult p = Ed.GetEntity("click on an entity");
G_OBJ =(Object) p;
G_OBJID = p.ObjectId;
if(G_OBJID.ToString() != "(0)")
G_ENT = (Entity)Trans.GetObject(G_OBJID, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
G_ENTTYPE = G_ENT.GetType().ToString();
if (G_ENTTYPE == "Autodesk.AutoCAD.DatabaseServices.Line")
G_LINE = (Line) G_ENT;
Point3d p0 = G_LINE.StartPoint, p1 = G_LINE.EndPoint;
double dX, dY, Len2D;
dX = (p1.X - p0.X); dY = (p1.Y - p0.Y);
Len2D = Math.Sqrt((dX * dX)+(dY * dY));
s += "\n Length (2D = " + Len2D + ") ";
else es += "\n Selected Object is not a Line";
catch(System.Exception EX){es += "\nErr.. in Get_Object : " + EX.Message.ToString();}

What is the problem.
it's urgent.

thanqs in advance
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
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.