protected override void SolveInstance(IGH_DataAccess DA) { string inputType = ""; if (!DA.GetData("SpeckleObjectType", ref inputType)) { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "No type specified."); return; } SpeckleObject inputObject = null; if (!DA.GetData("SpeckleObject", ref inputObject)) { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "No input object."); return; } // Try to find type in SpeckleInitializer Type objType = SpeckleCore.SpeckleInitializer.GetTypes().FirstOrDefault(t => t.Name == inputType); if (objType == null) { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Could not find SpeckleObject of type " + inputType + "."); return; } SpeckleObject convertedObject = (SpeckleObject)Activator.CreateInstance(objType); // Check to see if one is a subclass of another if (!(inputObject.Type.Contains(convertedObject.Type)) && !(convertedObject.Type.Contains(inputObject.Type))) { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "SpeckleObject not convertible to type."); return; } foreach (PropertyInfo p in convertedObject.GetType().GetProperties().Where(p => p.CanWrite)) { PropertyInfo inputProperty = inputObject.GetType().GetProperty(p.Name); if (inputProperty != null) { p.SetValue(convertedObject, inputProperty.GetValue(inputObject)); } } convertedObject.GenerateHash(); DA.SetData("Result", convertedObject); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { if (SelectedType is null) { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "No type chosen."); return; } object inputObject = null; if (!DA.GetData(0, ref inputObject)) { return; } try { inputObject = inputObject.GetType().GetProperty("Value").GetValue(inputObject); } catch { } if (inputObject == null) { return; } if (!(inputObject.GetType().IsSubclassOf(typeof(SpeckleObject)))) { inputObject = Converter.Serialise(inputObject); } SpeckleObject convertedObject = (SpeckleObject)Activator.CreateInstance(SelectedType); string inputObjectTypeString = (string)inputObject.GetType().GetProperty("Type").GetValue(inputObject); // Check to see if one is a subclass of another if (!((inputObjectTypeString.Contains(convertedObject.Type))) && !(convertedObject.Type.Contains(inputObjectTypeString))) { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "SpeckleObject not convertible to type."); return; } var deepCopyObject = CreateCopy(inputObject); foreach (PropertyInfo p in convertedObject.GetType().GetProperties().Where(p => p.CanWrite)) { PropertyInfo inputProperty = deepCopyObject.GetType().GetProperty(p.Name); if (inputProperty != null) { p.SetValue(convertedObject, inputProperty.GetValue(deepCopyObject)); } } convertedObject.GenerateHash(); // applicationId generation/setting var appId = convertedObject.GetType().GetProperty("ApplicationId").GetValue(convertedObject); if (appId == null) { var myGeneratedAppId = "gh/" + convertedObject.GetType().GetProperty("Hash").GetValue(convertedObject); convertedObject.GetType().GetProperty("ApplicationId").SetValue(convertedObject, myGeneratedAppId); } DA.SetData(0, convertedObject); }