/// <summary> /// Invoke the Components Create Method /// </summary> /// <param name="component"></param> public static void Build(this Grevit.Types.Component component, bool useReferenceElement) { using (DocumentLock acLckDoc = Grevit.AutoCad.Command.Document.LockDocument()) { // Create a new transaction Transaction transaction = Grevit.AutoCad.Command.Database.TransactionManager.StartTransaction(); using (transaction) { // Get the components type Type type = component.GetType(); // Get the Create extension Method using reflection IEnumerable <System.Reflection.MethodInfo> methods = Grevit.Reflection.Utilities.GetExtensionMethods(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Assembly, type); // Check all extensions methods (should only be Create() anyway) foreach (System.Reflection.MethodInfo method in methods) { // get the methods parameters object[] parameters = new object[method.GetParameters().Length]; // As it is an extension method, the first parameter is the component itself parameters[0] = component; parameters[1] = transaction; #region usingReferenceElement // if we should use a reference element to invoke the Create method // and parameter length equals 2 // get the components referenceGID, see if it has been created already // use this element as a parameter to invoke Create(Element element) if (useReferenceElement && parameters.Length == 3) { // Get the components reference GID System.Reflection.PropertyInfo propertyReferenceGID = type.GetProperty("referenceGID"); // Return if there is no reference GID property if (propertyReferenceGID == null) { return; } // Get the referene GID string value string referenceGID = (string)propertyReferenceGID.GetValue(component); // If the reference has been created already, get // the Element from the document and apply it as parameter two if (Command.created_objects.ContainsKey(referenceGID)) { DBObject referenceElement = transaction.GetObject(Command.created_objects[referenceGID], OpenMode.ForRead); parameters[2] = referenceElement; } } #endregion // If the create method exists if (method != null && method.Name.EndsWith("Create")) { // Invoke the Create Method without parameters DBObject createdElement = (Entity)method.Invoke(component, parameters); // If the return value is valud set the parameters if (createdElement != null) { component.SetParameters(createdElement); createdElement.AddXData(component, transaction); component.StoreGID(createdElement.Id); } } } // commit and dispose the transaction transaction.Commit(); } } }