I want to add a couple of properties in the symbol model preferable in the designer so I can set them during symbol editing via the property grid, what would be the recommended way to do this?
I want to add a couple of properties in the symbol model preferable in the designer so I can set them during symbol editing via the property grid, what would be the recommended way to do this?
The Essential Diagram SymbolModel class implements the Diagram.IPropertyContainer interface and to add properties that will be confined to programmatic access you can use the IPropertyContainer.SetPropertyValue(String propertyname, object propertyvalue) method to add the required properties anytime after a SymbolModel instance is created by the SymbolDesigner. The SymbolDesigner creates a new SymbolModel in response to the 'Add Symbol' command from within the 'MainForm.SymbolAdd_Click()' event handler found in the SymbolDesigner project.
// From the SymbolDesigner’s MainForm.SymbolAdd_Click() handler
C#
private void SymbolAdd_Click(object sender, System.EventArgs e) { if(curPalette != null) { SymbolModel symbolMdl = curPalette.AddSymbol("New Symbol"); symbolMdl.SetPropertyValue("Custom Property", "Test Value"); curPalette.AppendChild(symbolMdl); SymbolDocument formSymbolDoc = new SymbolDocument(symbolMdl); //... } }
VB
Private Sub SymbolAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not curPalette Is Nothing Then Dim symbolMdl As SymbolModel = curPalette.AddSymbol("New Symbol") symbolMdl.SetPropertyValue("Custom Property", "Test Value") curPalette.AppendChild(symbolMdl) Dim formSymbolDoc As SymbolDocument = New SymbolDocument(symbolMdl) '... End If End Sub
This property will be persisted along with the SymbolModel and you can subsequently use the SymbolModel’s IPropertyContainer.GetPropertyValue(String propertyname) method to retrieve this property from the SymbolModel when creating the Symbol.
However to add properties to the SymbolModel that can be edited through the SymbolDesigner's property editor, you will have to first implement a subclass of the Diagram.SymbolModel type that defines the extra properties that you require. This custom SymbolModel should be implemented as a serializable class that includes the 'Serializable' attribute and implements the requisite serialization constructor and GetObjectData(SerializationInfo, StreamingContext) method. Now within the SymbolDesigner’s 'Add Symbol' event handler, in place of the default SymbolModel.AddSymbol() method, create an instance of your custom SymbolModel class, and append this custom symbol model instance to the SymbolPalette using the SymbolPalette.AppendChild(INode mysymbolmodel) method. Also initialize the SymbolDocument that you will be creating with this new instance of the SymbolModel. The following code shows the revised handler,
C#
private void SymbolAdd_Click(object sender, System.EventArgs e) { Cursor prevCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; SymbolPalette curPalette = symbolPaletteGroupBar.CurrentPalette; if (curPalette != null) { CustomSymbolModel custsymbolmdl = new CustomSymbolModel(); curPalette.AppendChild(custsymbolmdl); SymbolDocument formSymbolDoc = new SymbolDocument(custsymbolmdl); formSymbolDoc.MdiParent = this; this.propertyEditor.Diagram = formSymbolDoc.Diagram; formSymbolDoc.Show(); } Cursor.Current = prevCursor; }
VB
Private Sub SymbolAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim prevCursor As Cursor = Cursor.Current Cursor.Current = Cursors.WaitCursor Dim curPalette As SymbolPalette = symbolPaletteGroupBar.CurrentPalette If Not curPalette Is Nothing Then Dim custsymbolmdl As CustomSymbolModel = New CustomSymbolModel() curPalette.AppendChild(custsymbolmdl) Dim formSymbolDoc As SymbolDocument = New SymbolDocument(custsymbolmdl) formSymbolDoc.MdiParent = Me Me.propertyEditor.Diagram = formSymbolDoc.Diagram formSymbolDoc.Show() End If Cursor.Current = prevCursor End Sub
The new properties for the SymbolModel will be available from the symbol model properties window, and may be accessed later on when using the SymbolModel to create a Symbol.
Please note that the subclassed SymbolModel type will have to be defined in a separate class library assembly, and both the SymbolDesigner and your application should link to this dll.