OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Custom Attributes on Shapes (Draw or Writer)

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Jul 06, 2004 7:56 am    Post subject: Custom Attributes on Shapes (Draw or Writer) Reply with quote

A few months ago I constructed some working examples of attaching custom attributes to shapes. (This is necessary for a WriterArt component.)

A conversation with Andrew Pitonyak was invaluable in getting this to work. As Andrew points out, it is absolutely necessary to assign the type as CDATA.

Here is how you would add a custom attribute to a shape. (On a Draw document.)

Code:
   oDoc = ThisComponent
   oDrawPage = oDoc.getDrawPages().getByIndex( 0 )
   ' get the only shape on the page.
   oShape = oDrawPage.getByIndex( 0 )
      
   oUserDefinedAttributes = oShape.UserDefinedAttributes
   
   oMyAttribute = createUnoStruct( "com.sun.star.xml.AttributeData" )
   With oMyAttribute
      .Type = "CDATA"
      .Value = "Meow Mix"
   End With
   oUserDefinedAttributes.insertByName( "Danny", oMyAttribute )
   oShape.UserDefinedAttributes = oUserDefinedAttributes


A new custom attribute "Danny" is attached to the shape having a value "Meow Mix". The custom attribute is even saved when the document containing the shape is saved.

You can later retrieve the information like this....

Code:
   oDoc = ThisComponent
   oDrawPage = oDoc.getDrawPages().getByIndex( 0 )
   oShape = oDrawPage.getByIndex( 0 )
      
   oUserDefinedAttributes = oShape.UserDefinedAttributes
   
   oMyAttribute = oUserDefinedAttributes.getByName( "Danny" )
   Print oMyAttribute.Value




In a Writer document, you would do something like this to add a custom attribute to a shape.

Code:
Sub Main
   oDoc = ThisComponent
   ' get the only draw page of a Writer doc
   oDrawPage = oDoc.getDrawPage()
   oShape = oDrawPage.getByIndex( 0 )
      
   oUserDefinedAttributes = oShape.UserDefinedAttributes
   
   oMyAttribute = createUnoStruct( "com.sun.star.xml.AttributeData" )
   With oMyAttribute
      .Type = "CDATA"
      .Value = "Meow Mix"
   End With
   oUserDefinedAttributes.insertByName( "Danny", oMyAttribute )
   oShape.UserDefinedAttributes = oUserDefinedAttributes
End Sub



Later, you can retrieve the custom attribute like this....

Code:
Sub Main
   oDoc = ThisComponent
   ' get the only draw page of a Writer doc
   oDrawPage = oDoc.getDrawPage()
   oShape = oDrawPage.getByIndex( 0 )
      
   oUserDefinedAttributes = oShape.UserDefinedAttributes
      
   oMyAttribute = oUserDefinedAttributes.getByName( "Danny" )
   Print oMyAttribute.Value
End Sub




If you take the Draw or Writer doucment with the custom attribute, and then unzip the OOo document and inspect the XML, you'll see that the shape has a custom XML attribute containing your special value.

If you copy/paste the shape between Draw and Writer, the custom attribute is copied/pasted with the shape! (This is very important for a WriterArt component!)

I would highly recommend using custom attributes that have names that absolutely will not collide with anyone else's.

For example, instead of naming the custom attribute "Danny", I will use many attributes that have names like....

name.dannyBrewer.writerArt.textInfo.text
name.dannyBrewer.writerArt.textInfo.font
name.dannyBrewer.writerArt.textInfo.size
etc., etc.
name.dannyBrewer.writerArt.shapeInfo.distortType
name.dannyBrewer.writerArt.shapeInfo.gradientFill
name.dannyBrewer.writerArt.shapeInfo.gradientStartColor
etc., etc.

This way, when a shape is selected, the WriterArt dialog can determine that the shape originally had certian text, so that it can create a new graphic of the same text, in the original font, etc.

This message is also an attempt to answer this question....
http://www.oooforum.org/forum/viewtopic.php?t=10486
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
j-rox
General User
General User


Joined: 14 May 2006
Posts: 30

PostPosted: Fri Aug 04, 2006 1:10 am    Post subject: Reply with quote

is there a chance to add UserDefinedAttributes to a text:h, text:p and text:span element?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group