.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to get an ObjectId ?

9 REPLIES 9
Reply
Message 1 of 10
Anonymous
531 Views, 9 Replies

How to get an ObjectId ?

Hi,

I would like to detach an XRef frow a drawing in a Autocad command.

I think that this code would work.
{code}
Document dwg = acadApp.DocumentManager.MdiActiveDocument;
Database db = dwg.Database;

db.DetachXref([An ObjectId]);
{code}

The problème is that I don't have the ObjectId.
I have the name and the path of the XRef.

So, How can I get the ObjectId ?
9 REPLIES 9
Message 2 of 10
michael_vanhoose
in reply to: Anonymous

here is the function I made to Detach Xref's


Public Overloads Shared Function XrefDetach(ByVal Xrefname As String) As Integer
Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument()
Using db As Database = HostApplicationServices.WorkingDatabase()
Using tr As Transaction = db.TransactionManager.StartTransaction

Try

Dim tbl As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
'Dim rec As BlockTableRecord = tr.GetObject(tbl(BlockTableRecord.ModelSpace), OpenMode.ForRead)

Dim bfound As Boolean = False

If tbl.Has(Xrefname) = True Then
Dim rec As BlockTableRecord = tr.GetObject(tbl.Item(Xrefname), OpenMode.ForWrite)

db.DetachXref(tbl.Item(Xrefname))
rec.Erase(True)

bfound = True
End If

db.ResolveXrefs(True, False)

tr.Commit()

If bfound = True Then
Return 1
Else
Return 2
End If

Catch ex As Exception
tr.Abort()
Return 0
End Try
End Using
End Using
End Using
End Function
Message 3 of 10
Anonymous
in reply to: Anonymous

Hi,

thank you for your sample. I tried to convert it into C#
{code}
// Get the current opened file as a dwg database
Document dwg = acadApp.DocumentManager.MdiActiveDocument;

//====Lock the document before starting your transaction=====
using (DocumentLock lck = dwg.LockDocument())
{
using (Database db = HostApplicationServices.WorkingDatabase)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
BlockTable tbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

if (tbl.Has(ReferenceName))
{
BlockTableRecord rec = tr.GetObject(tbl.GetField(ReferenceName), OpenMode.ForWrite) as BlockTableRecord;
db.DetachXref(tbl.GetField(ReferenceName));
rec.Erase(true);
}

db.ResolveXrefs(true, false);
tr.Commit();
}
catch (System.Exception e)
{
Dialogs.ShowError(e.Message);
tr.Abort();
}
}
}
}
//=====End of Document lock=====
{code}

The problem is that "tbl.Item()" doesn't exist. (working on AutoCAD 2009 in .Net)
I replace it with tbl.GetField();

There is no error; but nothing happens.
Message 4 of 10
Anonymous
in reply to: Anonymous

{quote}
I have the name and the path of the XRef.
{quote}

{code}
using (Transaction tr =
db.TransactionManager.StartTransaction())
{
BlockTable bt =
(BlockTable) tr.GetObject
(db.BlockTableId, OpenMode.ForRead);

db.DetachXref(bt[blockName]);

tr.Commit();
}
{code}

wrote in message news:6211274@discussion.autodesk.com...
Hi,

thank you for your sample. I tried to convert it into C#
{code}
// Get the current opened file as a dwg database
Document dwg = acadApp.DocumentManager.MdiActiveDocument;

//====Lock the document before starting your transaction=====
using (DocumentLock lck = dwg.LockDocument())
{
using (Database db = HostApplicationServices.WorkingDatabase)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
BlockTable tbl = tr.GetObject(db.BlockTableId,
OpenMode.ForRead) as BlockTable;

if (tbl.Has(ReferenceName))
{
BlockTableRecord rec =
tr.GetObject(tbl.GetField(ReferenceName), OpenMode.ForWrite) as
BlockTableRecord;
db.DetachXref(tbl.GetField(ReferenceName));
rec.Erase(true);
}

db.ResolveXrefs(true, false);
tr.Commit();
}
catch (System.Exception e)
{
Dialogs.ShowError(e.Message);
tr.Abort();
}
}
}
}
//=====End of Document lock=====
{code}

The problem is that "tbl.Item()" doesn't exist. (working on AutoCAD 2009 in
.Net)
I replace it with tbl.GetField();

There is no error; but nothing happens.
Message 5 of 10
Anonymous
in reply to: Anonymous

When translating VB to C# remember that C# uses indexers. The VB
IList.Item(0) is IList[0] in C#.

You can also get rid of some nesting with this syntax:

using (DocumentLock lck = dwg.LockDocument())
using (Database db = HostApplicationServices.WorkingDatabase)
using (Transaction tr = db.TransactionManager.StartTransaction())
{
...
}

However, you really shouldn't call dispose on a database that you didn't
instantiate. Autodesk has assured us that the dispose method is coded to
protect us from disposing of a database that we shouldn't, but it's still
not a good practice.

--
Bobby C. Jones
http://bobbycjones.spaces.live.com
Message 6 of 10
Anonymous
in reply to: Anonymous

Bobby,
why are you 'using' this
using (Database db = HostApplicationServices.WorkingDatabase)


??

/// kdub

"Bobby C. Jones" wrote in message
news:6211430@discussion.autodesk.com...
When translating VB to C# remember that C# uses indexers. The VB
IList.Item(0) is IList[0] in C#.

You can also get rid of some nesting with this syntax:

using (DocumentLock lck = dwg.LockDocument())
using (Database db = HostApplicationServices.WorkingDatabase)
using (Transaction tr = db.TransactionManager.StartTransaction())
{
...
}

However, you really shouldn't call dispose on a database that you didn't
instantiate. Autodesk has assured us that the dispose method is coded to
protect us from disposing of a database that we shouldn't, but it's still
not a good practice.

--
Bobby C. Jones
http://bobbycjones.spaces.live.com
Message 7 of 10
Anonymous
in reply to: Anonymous

Hey kdub,
I copied it over from the op's translation of another poster's VB code with
the intent of showing how to stack using statements instead of nesting them.

BTW - I have this urge to start calling you k-dawg, I'll try to restrain
myself 🙂
--
Bobby C. Jones
http://bobbycjones.spaces.live.com


"Kerry Brown" wrote in message
news:6211722@discussion.autodesk.com...
Bobby,
why are you 'using' this
using (Database db = HostApplicationServices.WorkingDatabase)


??

/// kdub
Message 8 of 10
kdub_nz
in reply to: Anonymous

{quote}
BTW - I have this urge to start calling you k-dawg, I'll try to restrain myself 🙂
{quote}

restraining yourself would be nice 🙂



OK, perhaps the using question should have been directed to michael.vanhoos...

I haven't checked the compiled code, perhaps the compiler wraps the block in a try/catch statement
.. but that isn't transparent.

I'd seriously like to know ...

/// kdub

// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 9 of 10
Anonymous
in reply to: Anonymous

There's actually nothing wrong with it now, but I think in
earlier releases, the dispose implementation didn't care
if the database was open in AutoCAD or not. Since I do
not always know what release someone may be using,
I generally advise to avoid it on databases that are open
in the editor.

Dispose can be safely called on most objects, since some
of them do nothing when it's called. The downside of doing
that is that code can become much more complicated.

But, one still cannot follow a general rule that you can/should
call Dispose() on anything that implements IDisposable (an
example is the Document object).

The problem we have with this API design, is that out of little
other than serving the convenience of the API developer,
almost all managed types derive from DisposableWrapper,
meaning they all implement IDisposable, without regard for
whether there is actually a need for it. The other problem with
the design is that in some cases, failing to call Dispose() can
be fatal.

Sorry to say it, but that's just a horrifyingly bad design
if you ask me, to wit the massive confusion we've seen
here from so many (expressed outright, and implied by
the code they post).

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");


"Kerry Brown" wrote in message
news:6211722@discussion.autodesk.com...
Bobby,
why are you 'using' this
using (Database db = HostApplicationServices.WorkingDatabase)


??

/// kdub

"Bobby C. Jones" wrote in message
news:6211430@discussion.autodesk.com...
When translating VB to C# remember that C# uses indexers. The VB
IList.Item(0) is IList[0] in C#.

You can also get rid of some nesting with this syntax:

using (DocumentLock lck = dwg.LockDocument())
using (Database db = HostApplicationServices.WorkingDatabase)
using (Transaction tr = db.TransactionManager.StartTransaction())
{
...
}

However, you really shouldn't call dispose on a database that you didn't
instantiate. Autodesk has assured us that the dispose method is coded to
protect us from disposing of a database that we shouldn't, but it's still
not a good practice.

--
Bobby C. Jones
http://bobbycjones.spaces.live.com
Message 10 of 10
Anonymous
in reply to: Anonymous

Thank you all. It works.

I use Paul Richardson's solution.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost