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

Getattribute problem

7 REPLIES 7
Reply
Message 1 of 8
zitsmi
1440 Views, 7 Replies

Getattribute problem

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
7 REPLIES 7
Message 2 of 8
norman.yuan
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
Message 3 of 8
zitsmi
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
Message 4 of 8
Anonymous
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
Message 5 of 8
zitsmi
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!!
Message 6 of 8
Anonymous
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!!
Message 7 of 8
zitsmi
in reply to: zitsmi

IT WORKS!!!

Thank you Tony, thank you Norman Yuan!!
Message 8 of 8
edson_luiz86
in reply to: Anonymous

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.

 

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