.NET

.NET

Reply
Valued Contributor
98 Posts
1 Kudo
Registered: ‎01-11-2012
Post 1 of 6
Accepted Solution

Create Object and get oject id or add to collection

499 Views, 5 Replies
11-08-2012 03:18 AM

Hello ppl,

 

I trying to create an autocad obbject eg: line. after creating i need to add object data to the created object.

unfortunately i am not able to get the objectid of the created object.

 

could some one help me with a code snippet on how to do this..

 

1. creating a object and add the created object to collect or selectionset

2. get the object id or refernece so i can attach object dat relavent to the objedt...

Regards,

Raghulan Gowthaman Cert IV TAA, B.E,.
Senior Technical Consultant | Developer - R&D
A2K Technologies Sydney
Web : www.a2ktechnologies.com.au
www.civil3dforum.com | www.e4forums.com
www.zcodia.com.au
www.raghulangowthaman.com

line.ObjectId is what you need (after the line being added into drawing database).

Valued Mentor
370 Posts
175 Kudos
Registered: ‎05-06-2012
Post 2 of 6

Re: Create Object and get oject id or add to collection

11-08-2012 07:29 AM in reply to: raghulan

Your question makes it somewhat obvious that you're new to AutoCAD .NET programming.  

 

My suggestion would be to download the learning materials Autodesk provides, as they include examples of the basic tasks you've described, and study them.

 

 

*Expert Elite*
1,087 Posts
217 Kudos
Registered: ‎04-27-2009
Post 3 of 6

Re: Create Object and get oject id or add to collection

11-08-2012 07:38 AM in reply to: raghulan

What do you actually mean by "not able to get objectid of created object"? How do you create the object? After you created, do you append it into a proper layout block (modelspace or paperspace) and add it to current transaction?

 

A newly created Entity object (Line, Circle...) is only assigned an ObjectId when being appended to a Layouy block.

 

So, you need to do your task in 2 steps:

 

1. create the needed Entity and append it to appropriate space (model/paper space) and commit the transaction, so that the entity become database residing object. Then you pass the entity's ObjecId to step 2

 

2. attach ObjectData to the entity, identified by the ObjectId passed in.

Valued Contributor
98 Posts
1 Kudo
Registered: ‎01-11-2012
Post 4 of 6

m

11-08-2012 02:08 PM in reply to: norman.yuan

Thanks Norman for your advice.

I am indeed a newbie but I know few things..

Sorry if I didnt explain clearly...

 

I know I can only get the object ID after I append the table...all I want is how to get the object ID of last created object.

===============================================================================

//open block table
BlockTable blktbl;


blktbl = tr.GetObject(Global.variables.db.BlockTableId, caddb.OpenMode.ForRead) as BlockTable;

 

//open blk table for write
BlockTableRecord blktblrec = tr.GetObject(blktbl[BlockTableRecord.ModelSpace], caddb.OpenMode.ForWrite) as BlockTableRecord;

 

//create line
Autodesk.AutoCAD.DatabaseServices.Line line = new Autodesk.AutoCAD.DatabaseServices.Line(startPoint, EndPoint);
line.Layer = Global.variables.pipes_layer; //get the layer name

line.SetDatabaseDefaults();

//add new object to the block table
blktblrec.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);

=================================================================================

 

please advice me how to get the object id of last created object..

 

Thanks all

Regards,

Raghulan Gowthaman Cert IV TAA, B.E,.
Senior Technical Consultant | Developer - R&D
A2K Technologies Sydney
Web : www.a2ktechnologies.com.au
www.civil3dforum.com | www.e4forums.com
www.zcodia.com.au
www.raghulangowthaman.com
*Expert Elite*
1,087 Posts
217 Kudos
Registered: ‎04-27-2009
Post 5 of 6

Re: m

11-08-2012 02:26 PM in reply to: raghulan

line.ObjectId is what you need (after the line being added into drawing database).

Valued Contributor
98 Posts
1 Kudo
Registered: ‎01-11-2012
Post 6 of 6

Re: get lat created object

11-08-2012 03:01 PM in reply to: norman.yuan

Thanks Norman, 

 

I was trying to get line.objectId before tr.commit() thats why I was getting "0"

 

just for some one who may need

 

======================

 

//open block table
BlockTable blktbl;
blktbl = tr.GetObject(Global.variables.db.BlockTableId, caddb.OpenMode.ForRead) as BlockTable;

//open blk table for write
BlockTableRecord blktblrec = tr.GetObject(blktbl[BlockTableRecord.ModelSpace], caddb.OpenMode.ForWrite) as BlockTableRecord;

//create line
Autodesk.AutoCAD.DatabaseServices.Line line = new Autodesk.AutoCAD.DatabaseServices.Line(startPoint, EndPoint);
//line.Layer = Global.variables.pipes_layer;

line.SetDatabaseDefaults();
Global.variables.mess_out(line.ObjectId.ToString());
//add new object to the block table
blktblrec.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);

tr.Commit(); // make sure you commit

 

line.ObjectId //will fetch you the object id of the line object.

 

=========================

 

Regards,

Raghulan Gowthaman Cert IV TAA, B.E,.
Senior Technical Consultant | Developer - R&D
A2K Technologies Sydney
Web : www.a2ktechnologies.com.au
www.civil3dforum.com | www.e4forums.com
www.zcodia.com.au
www.raghulangowthaman.com
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!