DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Tue Jul 06, 2004 7:56 am Post subject: Custom Attributes on Shapes (Draw or Writer) |
|
|
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? |
|