Getting Constant Attributes from a Block with C#

Getting Constant Attributes from a Block with C#

Anonymous
Not applicable
3,418 Views
14 Replies
Message 1 of 15

Getting Constant Attributes from a Block with C#

Anonymous
Not applicable
I am trying to get all of the attributes off of a given block entity.
I use the code below to pull off the array of attributes and it works fine.

//Create a reference to the block and open it for reading
BlockReference myBR = (BlockReference)tm.GetObject(aID, OpenMode.ForRead);

//Get the attributes off of the block reference
Autodesk.AutoCAD.DatabaseServices.AttributeCollection blockAttrs = myBR.AttributeCollection;

How do I get the hidden attributes off of the same block?
In the VBA version of this progam I used: varAttributes = blkRef.GetConstantAttributes

I cannot find the corresponding method to do this in C#.
0 Likes
3,419 Views
14 Replies
Replies (14)
Message 2 of 15

Anonymous
Not applicable
Constant attributes are not stored with the insertion, they're stored in the
block's definition.

You shouldn't be using constant attributes in most scenarios because the are
essentially nothing more than text objects in the block, that have special
meaning to attribute extraction (or the legacy version of it).

What are you using constant attributes for?

--
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");


wrote in message
news:[email protected]...
I am trying to get all of the attributes off of a given block entity. I use
the code below to pull off the array of attributes and it works fine.
//Create a reference to the block and open it for reading BlockReference
myBR = (BlockReference)tm.GetObject(aID, OpenMode.ForRead); //Get the
attributes off of the block reference
Autodesk.AutoCAD.DatabaseServices.AttributeCollection blockAttrs =
myBR.AttributeCollection; How do I get the hidden attributes off of the same
block? In the VBA version of this progam I used: varAttributes =
blkRef.GetConstantAttributes I cannot find the corresponding method to do
this in C#.
0 Likes
Message 3 of 15

Anonymous
Not applicable
Each block has two hidden attributes assigned that contain the block Name and short description.
These attributes are placed by the block creator, are never changed and are not displayed to the end-users.
Not my setup, just they way it's been done.

In VBA it was pretty simple, just used the two Subs:

'Put the attributes from the symbol into an array
attArray = blkRef.GetAttributes

'Get the Constant attributes which also happen to be invisible
varAttributes = blkRef.GetConstantAttributes

Are you implying that instead of using the block reference, I need to open the block definition?
How would I go about doing that?
0 Likes
Message 4 of 15

Anonymous
Not applicable
GetConstantAttributes() will get them, but it has to work much harder than
is necessary to get the attributes.

--
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");


wrote in message
news:[email protected]...
Each block has two hidden attributes assigned that contain the block Name
and short description. These attributes are placed by the block creator, are
never changed and are not displayed to the end-users. Not my setup, just
they way it's been done. In VBA it was pretty simple, just used the two
Subs: 'Put the attributes from the symbol into an array attArray =
blkRef.GetAttributes 'Get the Constant attributes which also happen to be
invisible varAttributes = blkRef.GetConstantAttributes Are you implying that
instead of using the block reference, I need to open the block definition?
How would I go about doing that?
0 Likes
Message 5 of 15

Anonymous
Not applicable
There is no GetConstantAttributes() method for the:
BlockReference or
AttributeReference or
AttributeDefinition or
AttributeCollection.

Has no one ever had to do this before?
0 Likes
Message 6 of 15

Anonymous
Not applicable
Try exploring the API in the object browser, and you'll find it.

--
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");


wrote in message
news:[email protected]...
There is no GetConstantAttributes() method for the: BlockReference or
AttributeReference or AttributeDefinition or AttributeCollection. Has no one
ever had to do this before?
0 Likes
Message 7 of 15

Anonymous
Not applicable
I have the block reference from:
Autodesk.AutoCAD.DatabaseServices.BlockReference

The GetConstantAttributes() method is on the:
Autodesk.AutoCAD.Interop.Common.IAcadBlockReference

I don't see any way to get from one to the other.
If it's not doable then why not just say so.
0 Likes
Message 8 of 15

Anonymous
Not applicable
The method is on the AcadBlock object.

--
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");


wrote in message
news:[email protected]...
I have the block reference from:
Autodesk.AutoCAD.DatabaseServices.BlockReference The GetConstantAttributes()
method is on the: Autodesk.AutoCAD.Interop.Common.IAcadBlockReference I
don't see any way to get from one to the other. If it's not doable then why
not just say so.
0 Likes
Message 9 of 15

Anonymous
Not applicable
>I don't see any way to get from one to the other.

Google is your friend.

Kean explains how to use com objects to get methods and properties not present in the net objects.

http://through-the-interface.typepad.com/through_the_interface/2007/02/using_the_com_i.html

He talks about a hatch object in this lesson but it works for all Acad types.

I used :

AcadBlockReference Ablkref = obj2 as AcadBlockReference;

to get the GetConstantAttributes() method.

Bill
0 Likes
Message 10 of 15

Anonymous
Not applicable
Sorry, my mistake.

The GetConstantAttributes() method is on the AcadBlockReference COM object.

You get that from the BlockReference's AcadObject property:

BlockReference blockref = ....

AcadBlockReference acadBlockRef = (AcadBlockReference)
blockref.AcadObject;

Then you can call acadBlockRef.GetConstantAttributes()

--
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");


wrote in message
news:[email protected]...
I have the block reference from:
Autodesk.AutoCAD.DatabaseServices.BlockReference The GetConstantAttributes()
method is on the: Autodesk.AutoCAD.Interop.Common.IAcadBlockReference I
don't see any way to get from one to the other. If it's not doable then why
not just say so.
0 Likes
Message 11 of 15

Anonymous
Not applicable
Thanks Tony.

I'm glad this came up.

I'm assuming this is an improvement between net upgrades?
(I've recently went to VS2008X & net3.5 from 2.0)
Apparently you don't need the Marshalling anymore.

From Keans example (back when) you had to do all this:

Autodesk.AutoCAD.Interop.AcadApplication oAcad = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17");
object obj2 = oAcad.ActiveDocument.ObjectIdToObject(ent.ObjectId.OldId);
AcadBlockReference AblkRef = obj2 as AcadBlockReference;

It appears now that this is all it takes to do the same thing

AcadBlockReference AblkRef = (AcadBlockReference)ent.AcadObject;

Or was this just another way to accomplish the same ends?
What am I missing?

Looks like I'll be cleaning up some old code here.


Bill
0 Likes
Message 12 of 15

Anonymous
Not applicable
You use Marshal.GetActiveObject() when you're connecting to AutoCAD from
another process. That (or CreateObject) is the only way to connect to
AutoCAD from another process.

You should never use GetActiveObject() from an in-process client (a
NETLOADed .dll), because the instance of AutoCAD you want to work with is
returned by the managed Application object's AcadApplication property, and
any other COM objects can be obtained directly from their corresponding
managed wrappers via the managed wrapper's AcadObject property.

There is also the AcadDocument and AcadDatabase properties of the Document
and Database managed wrappers respectively, that return the corresponding
COM wrappers.

--
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");


wrote in message news:[email protected]...
Thanks Tony. I'm glad this came up. I'm assuming this is an improvement
between net upgrades? (I've recently went to VS2008X & net3.5 from 2.0)
Apparently you don't need the Marshalling anymore. From Keans example (back
when) you had to do all this: Autodesk.AutoCAD.Interop.AcadApplication oAcad
= (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17"); object
obj2 = oAcad.ActiveDocument.ObjectIdToObject(ent.ObjectId.OldId);
AcadBlockReference AblkRef = obj2 as AcadBlockReference; It appears now that
this is all it takes to do the same thing AcadBlockReference AblkRef =
(AcadBlockReference)ent.AcadObject; Or was this just another way to
accomplish the same ends? What am I missing? Looks like I'll be cleaning up
some old code here. Bill
0 Likes
Message 13 of 15

Anonymous
Not applicable
Thanks,
that explains it.

I'd be interested in knowing some uses for com on the document and database.



Bill
0 Likes
Message 14 of 15

Anonymous
Not applicable
Bill,
Thank you for finally getting Tony to spill the beans about how to get this done.
I have spent several hours over the past several days trying to work this out.

FYI: our Internet access is filtered and any page that is classified as "personal/Blog" is automatically blocked.
So I cannot get to the reference you cited.

Tony's caddzone.com site comes up with a classification of "none" so of course it is off limits too.
Getting a waiver is a govenmental morass.

I have to work within these limitations.....
0 Likes
Message 15 of 15

Anonymous
Not applicable
Don't thank me, thank Tony.
I didn't do anything.

This really isn't a forum for learning how to program as much as it is with problems related to the net framework,
but Tony and others have been very generous with their time helping people.
Sometimes it's not what you ask but how you ask it.

And there is alot of sample code here on the site although the search function does leave something to be desired.

I'm in the same boat with internet filters.
When I use google, I keep the keywords simple, like, "GetConstantAttribute, c#" or something similar
and I can't always access all the sites it returns but some I can and are very useful.

Also the Object.arx samples and docs are helpful.

Bill
0 Likes