using System; using System.IO; using System.Windows.Forms; namespace CS_SchemaAddin { public class Printer { public string m_FilePath; public StreamWriter writer; private EA.SchemaComposer composer; public Printer( EA.SchemaComposer schema, string path) { composer = schema; m_FilePath = path; writer = File.CreateText(path); } public void Close() { writer.Close(); } string GetTypeName(int typeID) { string typeName = ""; EA.ModelType type = composer.FindInModel(typeID); if (type != null) { typeName = type.TypeName; } return typeName; } string GetQTypeName(int parentID, int typeID, int propTypeID) { string typeName = ""; if (parentID != typeID) typeName = GetTypeName(parentID) + "." + GetTypeName(propTypeID); else typeName = GetTypeName(propTypeID); return typeName; } public void PrintProperty( int typeID, EA.SchemaProperty prop) { writer.WriteLine(" name: " + prop.Name); if (prop.TypeID == 0) writer.WriteLine(" Type: " + prop.PrimitiveType); else writer.WriteLine(" Type: " + GetQTypeName(prop.Parent, typeID, prop.TypeID)); writer.WriteLine(" aggregation: " + prop.Aggregation.ToString()); writer.WriteLine(" cardinality: " + prop.Cardinality); writer.WriteLine(" containment: " + prop.Containment); writer.WriteLine(" initialValue: " + prop.InitialValue); writer.Flush(); } public void PrintProperties(EA.SchemaType Type) { EA.SchemaPropEnum propEnum = Type.Properties; if (propEnum != null) { int count = propEnum.GetCount(); writer.WriteLine(" - property count= " + count.ToString()); EA.SchemaProperty Prop = propEnum.GetFirst(); while (Prop != null) { PrintProperty(Type.TypeID, Prop); Prop = propEnum.GetNext(); } } } public void PrintInheritance(EA.ModelType modelType) { writer.WriteLine(" inheritance" ); EA.ModelTypeEnum supe = modelType.GetSuperclassEnum(EA.SearchType.searchBreadthFirst ); if (supe != null) { writer.WriteLine(" superclasses:"); EA.ModelType super = supe.GetFirst(); while (super != null) { writer.WriteLine(" " + super.TypeName); super = supe.GetNext(); } } EA.ModelTypeEnum sube = modelType.GetSubclassEnum(EA.SearchType.searchBreadthFirst); if (sube != null) { writer.WriteLine(" subclasses:"); EA.ModelType sub = sube.GetFirst(); while (sub != null) { writer.WriteLine(" " + sub.TypeName); sub = sube.GetNext(); } } } public void Print(EA.SchemaType schemaType) { EA.ModelType umlType = composer.FindInModel(schemaType.TypeID); string strTypeName = schemaType.TypeName; string strTypeGuid = schemaType.GUID; string strRoot = schemaType.GetFacet("root"); string strClassifier = umlType.ClassifierPath; writer.WriteLine(strTypeName + " = " + strTypeGuid); writer.WriteLine("classifier path: " + strClassifier); writer.WriteLine("root element: " + strRoot); PrintProperties(schemaType); PrintInheritance(umlType); } } public class Main { public Form1 theForm; public Main() { } //Called Before EA starts to check Add-In Exists public String EA_Connect(EA.Repository Repository) { //No special processing required. return "SchemaComposerAddin"; } //Called when user Click Add-Ins Menu item from within EA. //Populates the Menu with our desired selections. public object EA_GetMenuItems(EA.Repository Repository, string Location, string MenuName) { EA.Package aPackage = Repository.GetTreeSelectedPackage(); switch (MenuName) { case "": return "-&SchemaComposerAddin"; case "-&SchemaComposerAddin": string[] ar = { "About..." }; return ar; } return ""; } //Sets the state of the menu depending if there is an active project or not bool IsProjectOpen(EA.Repository Repository) { try { EA.Collection c = Repository.Models; return true; } catch { return false; } } //Called once Menu has been opened to see what menu items are active. public void EA_GetMenuState(EA.Repository Repository, string Location, string MenuName, string ItemName, ref bool IsEnabled, ref bool IsChecked) { IsChecked = false; if (IsProjectOpen(Repository)) IsEnabled = true; else IsEnabled = false; } //Called when user makes a selection in the menu. //This is your main exit point to the rest of your Add-in public void EA_MenuClick(EA.Repository Repository, string Location, string MenuName, string ItemName) { switch (ItemName) { case "About...": Form1 anAbout = new Form1(); anAbout.ShowDialog(); break; } } public bool EA_IsSchemaExporter(EA.Repository Repository, ref string displayName) { displayName = "MyModelPublisher"; return true; } public void EA_GetProfileInfo(EA.Repository Repository, EA.SchemaProfile profile) { profile.SetProperty("namespace", "MyNamespace"); profile.SetProperty("modelref", "Model Reference"); if (!profile.SetCapability("allowRootElement", true)) { MessageBox.Show("unrecognized parameter"); } if (!profile.SetCapability("allowCardinality", true)) { MessageBox.Show("unrecognized parameter"); } if (!profile.SetCapability("allowPropByRef", true)) { MessageBox.Show("unrecognized parameter"); } //composer.AddCustomCapability("something", "Friendly Name", "Y", "MYFUNCTIONNAME"); profile.AddExportFormat("My XML Schema"); profile.AddExportFormat("My RDF Schema"); profile.AddExportFormat("My JSON Schema"); } /* public bool EA_GetExporterInfo(EA.Repository Repository, ref string strCapabilites, ref string defNS, ref string strExports) { defNS = "MyNamespace"; strCapabilites = "allowRootElement=Y;allowCardinality=Y;allowPropByRef=Y"; strExports = "XML Schema(XSD)"; return true; } */ public bool EA_GenerateFromSchema(EA.Repository Repository, EA.SchemaComposer composer, string exports) { EA.SchemaTypeEnum e = composer.SchemaTypes; if (e != null) { long count = e.GetCount(); if (count > 0) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Multiselect = false; dlg.CheckFileExists = false; dlg.CheckPathExists = true; if(dlg.ShowDialog() == DialogResult.OK) { Printer printer = new Printer(composer,dlg.FileName); EA.SchemaType type = e.GetFirst(); while (type != null) { printer.Print(type); type = e.GetNext(); } printer.Close(); } } } return true; } } }