private static ElementGroupPrototype MakePrototype(GadgeteerHardware element) { var elementGroup = new ElementGroup(element.Store.DefaultPartition); elementGroup.AddGraph(element, true); return(elementGroup.CreatePrototype()); }
/* There are two versions of CreateElementToolPrototype here. * One deals with each Component subtype separately: if you add a new subtype, you need to add more here. * The other automatically deals with eacn Component subtype, using DSL reflection info. * * See http://msdn.microsoft.com/library/bb126279.aspx#groups */ /* * /// <summary> * /// Toolbox initialization, called for each element tool on the toolbox. * /// This version deals with each Component subtype separately. * /// </summary> * /// <param name="store"></param> * /// <param name="domainClassId">Identifies the domain class this tool should instantiate.</param> * /// <returns>prototype of the object or group of objects to be created by tool</returns> * protected override ElementGroupPrototype CreateElementToolPrototype(Store store, Guid domainClassId) * { * * if (domainClassId == Resistor.DomainClassId) * { * Resistor resistor = new Resistor(store); * * // Must initialize these in order of initialization: * // DSLT v1 bug affecting derived relationships. * resistor.T1 = new ComponentTerminal(store); * resistor.T2 = new ComponentTerminal(store); * * resistor.T1.Name = "t1"; * resistor.T2.Name = "t2"; * * ElementGroup elementGroup = new ElementGroup(store.DefaultPartition); * elementGroup.AddGraph(resistor, true); * // AddGraph includes the embedded parts. * * return elementGroup.CreatePrototype(); * } * else if (domainClassId == Capacitor.DomainClassId) * { * Capacitor capacitor = new Capacitor(store); * * // Must initialize these in order of initialization: * // DSLT v1 bug affecting derived relationships. * capacitor.T1 = new ComponentTerminal(store); * capacitor.T2 = new ComponentTerminal(store); * * capacitor.T1.Name = "t1"; * capacitor.T2.Name = "t2"; * * ElementGroup elementGroup = new ElementGroup(store.DefaultPartition); * elementGroup.AddGraph(capacitor, true); * // AddGraph includes the embedded parts. * * return elementGroup.CreatePrototype(); * } * * else if (domainClassId == Transistor.DomainClassId) * { * Transistor transistor = new Transistor(store); * * // Must initialize these in order of initialization: * // DSLT v1 bug affecting derived relationships. * transistor.Base = new ComponentTerminal(store); * transistor.Collector = new ComponentTerminal(store); * transistor.Emitter = new ComponentTerminal(store); * * transistor.Base.Name = "base"; * transistor.Collector.Name = "collector"; * transistor.Emitter.Name = "emitter"; * * // Create an ElementGroup for the Toolbox. * ElementGroup elementGroup = new ElementGroup(store.DefaultPartition); * elementGroup.AddGraph(transistor, true); * // AddGraph includes the embedded parts * * return elementGroup.CreatePrototype(); * } * * else * { * return base.CreateElementToolPrototype(store, domainClassId); * } * * * } * */ /// <summary> /// Toolbox initialization for components with fixed embedded parts. /// This deals with all the subclasses of Component, adding an instance for each relationship derived from ComponentHasTerminals. /// The benefit of this approach is that it does not need to be adjusted for each new Component subclass. /// Called for each element tool on the toolbox. /// </summary> /// <param name="store"></param> /// <param name="domainClassId">Identifies the domain class this tool should instantiate.</param> /// <returns>prototype of the object or group of objects to be created by tool</returns> protected override ElementGroupPrototype CreateElementToolPrototype(Store store, Guid domainClassId) { // Called for each element tool on the toolbox. // Get the class meta info for this class. DomainClassInfo thisClassInfo = store.DomainDataDirectory.FindDomainClass(domainClassId); if (!thisClassInfo.IsDerivedFrom(Component.DomainClassId)) { // Not one of those we're interested in: defer to base method. return(base.CreateElementToolPrototype(store, domainClassId)); } else { // Construct an instance of this type. Use the constructor that takes a store and a variable number of property bindings. Component component = (Component)thisClassInfo.ImplementationClass. // get the actual class from the meta info InvokeMember( // method in System.Reflection.Type "", System.Reflection.BindingFlags.CreateInstance // invoke the constructor | System.Reflection.BindingFlags.OptionalParamBinding, // which has a params binding null, // no special binder null, //no target object since we want the constructor new object[] { store }); // Called like "new Resistor(store)" // Now add whatever ComponentTerminals it has. // Each Component subtype sources several subrelationships of ComponentHasComponentTerminal. // Go through the metainfo about each role the class plays in relationships. foreach (DomainRoleInfo role in thisClassInfo.LocalDomainRolesPlayed) { // Pick out the roles that are one end of a relationship to a Terminal. if (role.DomainRelationship.IsDerivedFrom(ComponentHasComponentTerminal.DomainClassId) && !role.DomainRelationship.ImplementationClass.IsAbstract) { ComponentTerminal terminal = new ComponentTerminal(store); // Fill in the instance name with the name of the role - just a convenience. // The role at the Terminal end of the relationship is the one called "T1" or "collector" or some such. terminal.Name = role.OppositeDomainRole.Name.ToLowerInvariant(); // Instantiate the relationship with the constructor that takes the component and terminal. role.DomainRelationship.ImplementationClass.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, null, new object[] { component, terminal }); } } // Create an Element Group Prototype containing this tree. ElementGroup elementGroup = new ElementGroup(store.DefaultPartition); elementGroup.AddGraph(component, true, true); // AddGraph includes the embedded elements. elementGroup.AddRange(component.ComponentTerminals, true); ElementGroupPrototype egp = elementGroup.CreatePrototype(); return(egp); } }
/// <summary> /// Create a serialized prototype for Activity to attach to the toolbox. /// </summary> /// <param name="toolboxItem"></param> /// <returns>The prototype</returns> public override ElementGroupPrototype InitializeToolboxItem(ModelingToolboxItem toolboxItem) { ElementGroup elementGroup = new ElementGroup(this.Store); ElementGroupPrototype proto = null; Debug.Assert(this.Store.TransactionManager.InTransaction, "Must be in transaction"); ISpySoft.SFSchemaLanguage.DomainModel.Activity instance = ISpySoft.SFSchemaLanguage.DomainModel.Activity.CreateActivity(this.Store); if (instance != null) { elementGroup.AddGraph(instance); proto = elementGroup.CreatePrototype(instance); } return(proto); }
/// <summary> /// Execute the command /// </summary> public void Exec() { ElementGroup elementGroup = new ElementGroup(_store); bool foundSome = false; foreach (object o in _elements) { // Pick out shapes representing Component model elements. ShapeElement element = o as ShapeElement; if (element != null && element.ModelElement != null || o is ModelElement) { ModelElement mel = element != null ? element.ModelElement : o as ModelElement; if (mel is Entity || mel is Operation || mel is Enumeration || mel is ClassImplementation || mel is ServiceContract || mel is Property) { // add the element and its embedded children to the group elementGroup.AddGraph(mel, true); foundSome = true; } } } if (!foundSome) { return; } // A DataObject carries a serialized version. System.Windows.Forms.IDataObject data = new System.Windows.Forms.DataObject(); data.SetData(elementGroup.CreatePrototype()); System.Windows.Forms.Clipboard.SetDataObject (data, // serialized clones of our selected model elements false, // we don’t want to export outside this application 10, // retry 10 times on failure 50); // waiting 50ms between retries }