.NET

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

Getattribute problem

752 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*
Posts: 893
Registered: ‎04-27-2009
Message 2 of 7 (752 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
Posts: 6
Registered: ‎05-28-2009
Message 3 of 7 (752 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 (752 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
Posts: 6
Registered: ‎05-28-2009
Message 5 of 7 (752 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 (752 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
Posts: 6
Registered: ‎05-28-2009
Message 7 of 7 (752 Views)

Re: Getattribute problem

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

Thank you Tony, thank you Norman Yuan!!

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you interested in helping shape the Autodesk Community?
We’re looking at a few different ways to improve the “All Forums” landing page and need your feedback! If interested, please take a few minutes to fill out the following Usability Study. Thank you for your time!

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community