.NET

Reply
Active Member
zitsmi
Posts: 6
Registered: ‎05-28-2009
Message 1 of 7 (836 Views)

Getattribute problem

836 Views, 6 Replies
05-28-2009 08:03 AM
Hi,

I have a big problem to obtain the attribute linked to a block reference:

//CODE
AcadDocuments objAcadDocs = objAcadApp.Documents;
AcadDocument objAcadDoc = objAcadDocs.Open(filePath, true, "");
AcadBlocks objAcadBlks = objAcadDoc.Blocks;
foreach (AcadEntity objAcadEnty in objAcadDoc.ModelSpace)
{
if (objAcadEnty is AcadBlockReference)
{
AcadBlockReference objAcadBlkRef = (AcadBlockReference)objAcadEnty;
if (objAcadBlkRef.HasAttributes == true)
{
AcadAttributeReference objAcadAttRef = (AcadAttributeReferenceClass)objAcadBlkRef.GetAttributes();
}
}
}

I'm using VS2005 with Autocad2004 (ActiveX API).
Thank for help
/Stefano
*Expert Elite*
norman.yuan
Posts: 1,063
Registered: ‎04-27-2009
Message 2 of 7 (836 Views)

Re: Getattribute problem

05-28-2009 10:44 AM in reply to: zitsmi
COM method AcadBlockReference.GetAttributes() returns an array of AcadAttributeReference, not a single AcadAttributeReference. This is why your doe is not working: you are trying to cast an array of AcadAttributeReference to a single AcadAttributeReference
Active Member
zitsmi
Posts: 6
Registered: ‎05-28-2009
Message 3 of 7 (836 Views)

Re: Getattribute problem

05-28-2009 11:46 PM in reply to: zitsmi
norman.yuan,

how can I cast the array from AcadBlockReference.GetAttributes() to an array of AcadAttributeReference?
I've tried a lot but without success: can you provide me the code?
Thank you for your help, I'm desperate!

/Stefano
*Norman Yuan
Message 4 of 7 (836 Views)

Re: Getattribute problem

05-29-2009 05:23 AM in reply to: zitsmi

It could be like this:

 

...

    if
(objAcadBlkRef.HasAttributes)

    {

        //Loop
through the attributs

        foreach
(AcadAttributeReference att as AcadAttributeReference in
objAcadBlkRef.GetAttributs())

       
(

       
    switch (att.TagString.ToUpper())

       
    {

       
        case "TAG1":

       
            ...

       
            break;

       
        case "TAG2"

       
            ...

       
            break;

       
        ...

       
    }

       
}

    }

   

Or

...

    if
(objAcadBlkRef.HasAttributes)
    {

        //Loop
through the attributs

        foreach
(object obj in objAcadBlkRef.GetAttributs())

       
(

           
AcadAttributeReference att=(AcadAttributeReference) obj;

       
    switch (att.TagString.ToUpper())

       
    {

       
        case "TAG1":

       
            ...

       
            break;

       
        case "TAG2"

       
            ...

               
    break;

               
...

       
    }

       
}

    }

 

Or

...

    if
(objAcadBlkRef.HasAttributes)
    {

        object[]
objs=objAcadBlkRef.GetAttributes();

        foreach (object obj in objs)

        {

           
AcadAttributeReference att=(AcadAttributeReference)obj;

            ...

        }

    }


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
norman.yuan,
how can I cast the array from AcadBlockReference.GetAttributes() to an array
of AcadAttributeReference? I've tried a lot but without success: can you
provide me the code? Thank you for your help, I'm desperate!
/Stefano
Active Member
zitsmi
Posts: 6
Registered: ‎05-28-2009
Message 5 of 7 (836 Views)

Re: Getattribute problem

05-29-2009 06:32 AM in reply to: zitsmi
Sorry Norman Yuan,
it still doesn't work.

Example n°1:
Syntax error

Example n°2:
I receive error --> foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

Example n°3 :
I receive error --> Cannot implicitly convert type 'object' to 'object[]'. An explicit conversion exists (are you missing a cast?)
It's like method GetAttributes return a single object, not an Array

Probably I'm confused about Autocad 2004 Object Model...
Thank you again!!
*Tony Tanzillo
Message 6 of 7 (836 Views)

Re: Getattribute problem

05-29-2009 06:43 AM in reply to: zitsmi
Try:

{code}

object[] attributes = (object[]) blockrefObj.GetAttributes()
foreach( AcadAttributeReference att in attributes )
{
// ...
}

{code}

--
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:6191850@discussion.autodesk.com...
Sorry Norman Yuan, it still doesn't work. Example n°1: Syntax error Example
n°2: I receive error --> foreach statement cannot operate on variables of
type 'object' because 'object' does not contain a public definition for
'GetEnumerator' Example n°3 : I receive error --> Cannot implicitly convert
type 'object' to 'object[]'. An explicit conversion exists (are you missing
a cast?) It's like method GetAttributes return a single object, not an Array
Probably I'm confused about Autocad 2004 Object Model... Thank you again!!
Active Member
zitsmi
Posts: 6
Registered: ‎05-28-2009
Message 7 of 7 (836 Views)

Re: Getattribute problem

05-29-2009 07:05 AM in reply to: zitsmi
IT WORKS!!!

Thank you Tony, thank you Norman Yuan!!
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.