Getattribute problem

Getattribute problem

Anonymous
Not applicable
2,054 Views
7 Replies
Message 1 of 8

Getattribute problem

Anonymous
Not applicable
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
0 Likes
2,055 Views
7 Replies
Replies (7)
Message 2 of 8

norman.yuan
Mentor
Mentor
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

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 8

Anonymous
Not applicable
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
0 Likes
Message 4 of 8

Anonymous
Not applicable

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
0 Likes
Message 5 of 8

Anonymous
Not applicable
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!!
0 Likes
Message 6 of 8

Anonymous
Not applicable
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!!
Message 7 of 8

Anonymous
Not applicable
IT WORKS!!!

Thank you Tony, thank you Norman Yuan!!
0 Likes
Message 8 of 8

edson_luiz86
Enthusiast
Enthusiast

Sorry for post on old post, but I've tried to conveting the object array of .GetAttributes() for a many ways yestarday without succes. Today this post help me so much tanks.

 

0 Likes