.NET

Reply
*Matt
Message 1 of 12 (240 Views)

How do i find a block in the BlockTable

240 Views, 11 Replies
05-23-2006 12:13 AM
Finally trying to get my hands dirty on .NET. It's not that easy really


How do i find a BlockTabelRecord from a name?

In objectArx i got the objectId using AcDbBlockTable::getAt(const ACHAR*
entryName,AcDbObjectId& recordId);
Then I created a new AcDbBlockReference using the id, added it to the
database and i was done.

In .NET I can not find any GetAt method. Do I have to iterate the enumerator
and find it by myself?


I'm to old for this new stuff. Guess i should stay with Arx and ADS ;-)

/Matt
Distinguished Contributor
RolandF
Posts: 172
Registered: ‎11-19-2003
Message 2 of 12 (240 Views)

Re: How do i find a block in the BlockTable

05-23-2006 01:45 AM in reply to: *Matt

I do it with the following code, but i don't know
if it is the best way:

 

public bool IsBlockInDrawing(string BlockName, out ObjectId BlockId)

{

bool BlockIsInDrawing = false;

ObjectId tmpID = new ObjectId();

BlockId = tmpID;

using (Transaction tr = db.TransactionManager.StartTransaction())

using (BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false))

{

foreach (ObjectId tmpBlockId in bt)

{

using (BlockTableRecord btr = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead, false))

{

if (String.Compare(btr.Name, BlockName, true) == 0)

{

BlockIsInDrawing =

true;

BlockId = tmpBlockId;

break;

}

}

}

tr.Commit();

return BlockIsInDrawing;

}

}


 


--
Roland Feletic
Ingenieurbuero A.
Pauser Ges.m.b.H.

size=2>http://www.pauser.at

 

hp workstation xw4200, 3GB
quadro
fx1400
AutoCAD 2007, 3DSMax 8.0 SP 2

Finally trying to get
my hands dirty on .NET. It's not that easy really


How do i find a
BlockTabelRecord from a name?

In objectArx i got the objectId using
AcDbBlockTable::getAt(const ACHAR*
entryName,AcDbObjectId&
recordId);
Then I created a new AcDbBlockReference using the id, added it to
the
database and i was done.

In .NET I can not find any GetAt method.
Do I have to iterate the enumerator
and find it by myself?


I'm to
old for this new stuff. Guess i should stay with Arx and ADS
;-)

/Matt
*Matt
Message 3 of 12 (240 Views)

Re: How do i find a block in the BlockTable

05-23-2006 05:23 AM in reply to: *Matt

Thanks.

But I found a much better way (after a
while):

 

BlockId  = bt[BlockName]

 

/Matt

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">


I do it with the following code, but i don't know
if it is the best way:

 

public bool IsBlockInDrawing(string BlockName, out ObjectId BlockId)

{

bool BlockIsInDrawing = false;

ObjectId tmpID = new ObjectId();

BlockId = tmpID;

using (Transaction tr = db.TransactionManager.StartTransaction())

using (BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false))

{

foreach (ObjectId tmpBlockId in bt)

{

using (BlockTableRecord btr = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead, false))

{

if (String.Compare(btr.Name, BlockName, true) == 0)

{

BlockIsInDrawing =

true;

BlockId = tmpBlockId;

break;

}

}

}

tr.Commit();

return BlockIsInDrawing;

}

}


 


--
Roland Feletic
Ingenieurbuero A.
Pauser Ges.m.b.H.

size=2>http://www.pauser.at

 

hp workstation xw4200, 3GB
quadro
fx1400
AutoCAD 2007, 3DSMax 8.0 SP 2

Finally trying to
get my hands dirty on .NET. It's not that easy really


How do i find
a BlockTabelRecord from a name?

In objectArx i got the objectId using
AcDbBlockTable::getAt(const ACHAR*
entryName,AcDbObjectId&
recordId);
Then I created a new AcDbBlockReference using the id, added it
to the
database and i was done.

In .NET I can not find any GetAt
method. Do I have to iterate the enumerator
and find it by
myself?


I'm to old for this new stuff. Guess i should stay with Arx
and ADS ;-)

/Matt
Distinguished Contributor
RolandF
Posts: 172
Registered: ‎11-19-2003
Message 4 of 12 (240 Views)

Re: How do i find a block in the BlockTable

05-23-2006 05:44 AM in reply to: *Matt

Great, much shorter now ;-)


--
Roland Feletic
Ingenieurbuero A. Pauser Ges.m.b.H.

href="http://www.pauser.at">http://www.pauser.at

 

hp workstation xw4200, 3GB
quadro fx1400
AutoCAD 2007, 3DSMax 8.0 SP
2
*perry
Message 5 of 12 (240 Views)

Re: How do i find a block in the BlockTable

05-23-2006 07:59 AM in reply to: *Matt
Matt wrote:
> Thanks.
> But I found a much better way (after a while):
>
> BlockId = bt[BlockName]
>
> /Matt
>
Be wary that this method WILL return erased records, whether you want them
or not. It is a serious bug in the .Net api which AutoDesk SHOULD fix.
Tony Tanzillo has done a good job of explaining this.
Perry
Distinguished Contributor
RolandF
Posts: 172
Registered: ‎11-19-2003
Message 6 of 12 (240 Views)

Re: How do i find a block in the BlockTable

05-23-2006 08:23 AM in reply to: *Matt

But this should help, shouldn't it?

 

if (!BlockId.IsErased)

BlockIsInDrawing =

true;



--
Roland Feletic
Ingenieurbuero A.
Pauser Ges.m.b.H.

size=2>http://www.pauser.at

 

hp workstation xw4200, 3GB
quadro
fx1400
AutoCAD 2007, 3DSMax 8.0 SP 2

Matt wrote:
>
Thanks.
> But I found a much better way (after a while):


> BlockId  = bt[BlockName]

>
/Matt

Be wary that this method WILL return erased records,
whether you want them
or not. It is a serious bug in the .Net api which
AutoDesk SHOULD fix.
Tony Tanzillo has done a good job of explaining
this.
Perry
*perry
Message 7 of 12 (240 Views)

Re: How do i find a block in the BlockTable

05-23-2006 08:54 AM in reply to: *Matt
RolandF wrote:
> But this should help, shouldn't it?
>
>
> if (!BlockId.IsErased)
>
> BlockIsInDrawing = true;
>
>
> --
> Roland Feletic
> Ingenieurbuero A. Pauser Ges.m.b.H.
> http://www.pauser.at

I also have a section of code that asks "if(!btr.isErased)" which returns
true, then when I attempt to open the record I get an "eWasErased" exception.
I dont trust the api in this regard. The only for sure method I know of is
to iterate the whole table, using foreach.
See the posts here "when is erased really erased" and "transaction, purge and
ewaserased".
*Tony Tanzillo
Message 8 of 12 (240 Views)

Re: How do i find a block in the BlockTable

05-23-2006 09:04 AM in reply to: *Matt
Have a look at this newsgroup, you'll find discussions
about the indexer behavior (it returns erased records).

To get a non-erased record you must iterate the table,
if the IsErased property of the ObjectId returned by the
indexer returns true.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

"Matt" wrote in message news:5182988@discussion.autodesk.com...
Thanks.
But I found a much better way (after a while):

BlockId = bt[BlockName]

/Matt

wrote in message news:5182890@discussion.autodesk.com...
I do it with the following code, but i don't know if it is the best way:

public bool IsBlockInDrawing(string BlockName, out ObjectId BlockId){bool BlockIsInDrawing = false;ObjectId tmpID = new ObjectId();BlockId = tmpID;using (Transaction tr = db.TransactionManager.StartTransaction())using (BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false)){foreach (ObjectId tmpBlockId in bt){using (BlockTableRecord btr = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead, false)){if (String.Compare(btr.Name, BlockName, true) == 0){BlockIsInDrawing = true;BlockId = tmpBlockId;break;}}}tr.Commit();return BlockIsInDrawing;}}

--
Roland Feletic
Ingenieurbuero A. Pauser Ges.m.b.H.
http://www.pauser.at

hp workstation xw4200, 3GB
quadro fx1400
AutoCAD 2007, 3DSMax 8.0 SP 2
"Matt" schrieb im Newsbeitrag news:5182844@discussion.autodesk.com...
Finally trying to get my hands dirty on .NET. It's not that easy really


How do i find a BlockTabelRecord from a name?

In objectArx i got the objectId using AcDbBlockTable::getAt(const ACHAR*
entryName,AcDbObjectId& recordId);
Then I created a new AcDbBlockReference using the id, added it to the
database and i was done.

In .NET I can not find any GetAt method. Do I have to iterate the enumerator
and find it by myself?


I'm to old for this new stuff. Guess i should stay with Arx and ADS ;-)

/Matt
*Tony Tanzillo
Message 9 of 12 (240 Views)

Re: How do i find a block in the BlockTable

05-23-2006 09:08 AM in reply to: *Matt
"perry" wrote

>> I also have a section of code that asks "if(!btr.isErased)"
>> which returns true, then when I attempt to open the record
>> I get an "eWasErased" exception.

Are you opening the record with the optional third
argument to GetObject() (e.g., true = open erased
object) ?


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com
*perry
Message 10 of 12 (240 Views)

Re: How do i find a block in the BlockTable

05-23-2006 09:42 AM in reply to: *Matt
Tony Tanzillo wrote:
> "perry" wrote
>
>
>>>I also have a section of code that asks "if(!btr.isErased)"
>>>which returns true, then when I attempt to open the record
>>>I get an "eWasErased" exception.
>
>
> Are you opening the record with the optional third
> argument to GetObject() (e.g., true = open erased
> object) ?
>
>
Heres the relevant snippet:
// Get the block table
BlockTable blkTbl = (BlockTable)(trans.GetObject(dBase.BlockTableId, OpenMode.ForRead));
BlockTableRecord blkTblRec = (BlockTableRecord)trans.GetObject(blkTbl[BlockTableRecord.PaperSpace], OpenMode.ForRead);
foreach (ObjectId objId in blkTblRec) //iterator should skip erased objects?
{
// For each object in the block table record, cast it to a block reference
DBObject dBaseObj = trans.GetObject(objId, OpenMode.ForRead);
BlockReference blkRef = dBaseObj as BlockReference;
if (blkRef != null && !blkRef.IsErased)//still getting erased !!!!!!!!!!!!
{...}

No, I didnt specifically include the "false" in the getobject statement. I thought the default
was false. Anyways, from previous discussion I was left with the impression that "getobject"
was faulty and returned erased records regardless.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community