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

How do i find a block in the BlockTable

11 REPLIES 11
Reply
Message 1 of 12
Anonymous
796 Views, 11 Replies

How do i find a block in the BlockTable

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
11 REPLIES 11
Message 2 of 12
Anonymous
in reply to: Anonymous


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
Message 3 of 12
Anonymous
in reply to: Anonymous


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
Message 4 of 12
Anonymous
in reply to: Anonymous


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
Message 5 of 12
Anonymous
in reply to: Anonymous

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
Message 6 of 12
Anonymous
in reply to: Anonymous


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
Message 7 of 12
Anonymous
in reply to: Anonymous

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".
Message 8 of 12
Anonymous
in reply to: Anonymous

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
Message 9 of 12
Anonymous
in reply to: Anonymous

"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
Message 10 of 12
Anonymous
in reply to: Anonymous

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.
Message 11 of 12
Anonymous
in reply to: Anonymous

If i delete all blockreferences in the drawing and then i purge the drawing
i do not get an error.
So the error just occours when i delete it in the programm, dosn't it?

--
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
Message 12 of 12
Anonymous
in reply to: Anonymous


Now i changed the function a little bit. This
should work now.

 

public bool IsBlockInDrawing(string BlockName, out ObjectId BlockId)

{

    bool BlockIsInDrawing = false;

    BlockId =

new ObjectId();

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

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

    {

        try

        {

            BlockId = bt[BlockName];

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

            {

                if (!btr.IsLayout && !btr.IsAnonymous)

                    BlockIsInDrawing =

true;

            }

        }

        catch

        {

        }

        tr.Commit();

        return BlockIsInDrawing;

    }

}



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

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

 

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

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