Beispiel #1
0
        private static void _transferParameters(RevitElem SourceElement, RevitElem DestinationElement)
        {
            RevitDB.ParameterSet sourceParameters = SourceElement.Parameters;

            foreach (RevitDB.Parameter sourceParam in sourceParameters)
            {
                if (sourceParam.IsReadOnly == false)
                {
                    RevitDB.Definition def = sourceParam.Definition;
                    RevitDB.Parameter  destinationParam = DestinationElement.get_Parameter(def);

                    RevitDB.StorageType st = sourceParam.StorageType;
                    switch (st)
                    {
                    case RevitDB.StorageType.Double:
                        destinationParam.Set(sourceParam.AsDouble());
                        break;

                    case RevitDB.StorageType.ElementId:
                        destinationParam.Set(sourceParam.AsElementId());
                        break;

                    case RevitDB.StorageType.Integer:
                        destinationParam.Set(sourceParam.AsInteger());
                        break;

                    case RevitDB.StorageType.String:
                        destinationParam.Set(sourceParam.AsString());
                        break;
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Changes the workset of an element.
        /// </summary>
        /// <param name="element">Dynamo Elements.</param>
        /// <param name="workset">A revit workset</param>
        /// <returns name="element">The element that was changed.  Returns null if the change was unsuccessfull.</returns>
        public static dynamoElement SetElementWorkset(dynamoElement element, Workset workset)
        {
            //Get Revit Document object
            revitDoc doc = DocumentManager.Instance.CurrentDBDocument;

            Autodesk.Revit.DB.Element unwrapped = element.InternalElement;

            WorksetId wId = unwrapped.WorksetId;

            Autodesk.Revit.DB.Parameter wsParam = unwrapped.get_Parameter(BuiltInParameter.ELEM_PARTITION_PARAM);
            if (wsParam == null)
            {
                return(null);
            }
            if (doc.IsModifiable)
            {
                wsParam.Set(workset.internalId.IntegerValue);
            }
            else
            {
                using (Autodesk.Revit.DB.Transaction tx = new Autodesk.Revit.DB.Transaction(doc))
                {
                    tx.Start("Change Element's Workset");
                    wsParam.Set(workset.internalId.IntegerValue);
                    tx.Commit();
                }
            }
            return(unwrapped.ToDSType(true));;
        }
        private void Load()
        {
            DirectoryInfo d        = new DirectoryInfo(_data.Setting.GetFolderPath());
            var           p        = d.GetFiles().ToList();
            string        filename = lb_view.SelectedItem.ToString();
            string        pathfile = "";

            foreach (var item in p)
            {
                if (item.Name.Replace(".json", "") == filename)
                {
                    pathfile = item.FullName;
                    break;
                }
            }
            var             json    = File.ReadAllText(pathfile);
            CegParameterSet dictemp = JsonConvert.DeserializeObject <CegParameterSet>(json);

            a.ViewSheet viewSheet = _doc.ActiveView as a.ViewSheet;
            using (a.Transaction tran = new a.Transaction(_doc, "Set Parameter Sheet"))
            {
                tran.Start();
                foreach (var item in dictemp.Parameters)
                {
                    foreach (var item2 in _data.dic.Parameters)
                    {
                        if (item.Value.Name == item2.Value.Name)
                        {
                            a.Parameter pa = viewSheet.LookupParameter(item2.Value.Name);
                            if (pa != null)
                            {
                                a.InternalDefinition definition = pa.Definition as a.InternalDefinition;
                                if (definition.BuiltInParameter != a.BuiltInParameter.SHEET_NAME && definition.BuiltInParameter != a.BuiltInParameter.SHEET_NUMBER)
                                {
                                    switch (item2.Value.Type)
                                    {
                                    case a.StorageType.Double:
                                        pa.Set(item.Value.AsDouble);
                                        break;

                                    case a.StorageType.String:
                                        pa.Set(item.Value.AsString);
                                        break;

                                    case a.StorageType.Integer:
                                        pa.Set(item.Value.AsInteger);
                                        break;

                                    default:
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                tran.Commit();
            }
            Close();
        }
Beispiel #4
0
        SetParameter(Revit.Parameter parameterToSet, Revit.ParameterSet parameterSet)
        {
            foreach (Revit.Parameter param in parameterSet)
            {
                if (param.Definition.Name == parameterToSet.Definition.Name)
                {
                    switch (param.StorageType)
                    {
                    case Revit.StorageType.Double:
                        parameterToSet.Set(param.AsDouble());
                        break;

                    case Revit.StorageType.ElementId:
                        Revit.ElementId elemId = param.AsElementId();
                        parameterToSet.Set(elemId);
                        break;

                    case Revit.StorageType.Integer:
                        parameterToSet.Set(param.AsInteger());
                        break;

                    case Revit.StorageType.String:
                        parameterToSet.Set(param.AsString());
                        break;
                    }
                    return;
                }
            }
        }
Beispiel #5
0
        public static RevitElem ModifyParameter(ParameterJSON paramJSON, RevitElem Elem)
        {
            RevitParam param = null;

            if (paramJSON.IsShared)
            {
                param = Elem.get_Parameter(new Guid(paramJSON.GUID));
            }
            else if (paramJSON.Id < 0)
            {
                param = Elem.get_Parameter((BuiltInParameter)paramJSON.Id);
            }
            else if (paramJSON.Id > 0)
            {
                revitDoc         doc       = Elem.Document;
                ParameterElement paramElem = (ParameterElement)doc.GetElement(new ElementId(paramJSON.Id));
                if (paramElem != null)
                {
                    Definition def = paramElem.GetDefinition();
                    param = Elem.get_Parameter(def);
                }
            }

            if (param != null && !param.IsReadOnly)
            {
                switch (paramJSON.StorageType)
                {
                case "Double":
                    param.Set(Convert.ToDouble(paramJSON.Value));
                    break;

                case "ElementId":
                    param.Set(new ElementId(Convert.ToInt32(paramJSON.Value)));
                    break;

                case "Integer":
                    param.Set(Convert.ToInt32(paramJSON.Value));
                    break;

                case "String":
                default:
                    param.Set(paramJSON.Value);
                    break;
                }
            }

            return(Elem);
        }
        public static RevitElem ModifyParameter(SerialParameter serialParameter, RevitElem Elem)
        {
            RevitParam param = null;
            RevitDoc   doc   = Elem.Document;

            if (serialParameter.IsShared)
            {
                param = Elem.get_Parameter(new Guid(serialParameter.GUID));
            }
            else if (serialParameter.Id < 0)
            {
                param = Elem.get_Parameter((BuiltInParameter)serialParameter.Id);
            }
            else if (serialParameter.Id > 0)
            {
                ParameterElement paramElem = (ParameterElement)doc.GetElement(new ElementId(serialParameter.Id));
                if (paramElem != null)
                {
                    Definition def = paramElem.GetDefinition();
                    param = Elem.get_Parameter(def);
                }
            }

            if (param != null && !param.IsReadOnly)
            {
                switch (serialParameter.StorageType)
                {
                case "Double":
                    param.Set(Convert.ToDouble(serialParameter.Value));
                    break;

                case "ElementId":
                    SerialParameter._ModifyElementIdParameter(param, serialParameter.ValueElemId, doc);
                    break;

                case "Integer":
                    param.Set(Convert.ToInt32(serialParameter.Value));
                    break;

                case "String":
                default:
                    param.Set(serialParameter.Value);
                    break;
                }
            }

            return(Elem);
        }
Beispiel #7
0
        /// <summary>
        /// Given a list of viewport element IDs, the function will get the viewport from the document and give each viewport a temporary sheet number.  Function will ignore legends.
        /// </summary>
        /// <param name="viewPortIds">Revit ElementId of the viewports.</param>
        /// <param name="doc">The Revit Document the viewports are in.</param>
        /// <returns name="viewports">Returns the Revit viewports.</returns>
        internal static List <revitViewport> _tempRenumberViewports(List <revitElemId> viewPortIds, revitDoc doc)
        {
            List <revitViewport> viewPorts = new List <revitViewport>();
            int i = 1;

            foreach (revitElemId id in viewPortIds)
            {
                revitViewport vp = (revitViewport)doc.GetElement(id);
                revitView     v  = (revitView)doc.GetElement(vp.ViewId);

                if (
                    v.ViewType == revitDB.ViewType.FloorPlan ||
                    v.ViewType == revitDB.ViewType.CeilingPlan ||
                    v.ViewType == revitDB.ViewType.Elevation ||
                    v.ViewType == revitDB.ViewType.ThreeD ||
                    v.ViewType == revitDB.ViewType.DraftingView ||
                    v.ViewType == revitDB.ViewType.AreaPlan ||
                    v.ViewType == revitDB.ViewType.Section ||
                    v.ViewType == revitDB.ViewType.Detail ||
                    v.ViewType == revitDB.ViewType.Rendering
                    )
                {
                    viewPorts.Add(vp);

                    revitDB.Parameter param = vp.get_Parameter(revitDB.BuiltInParameter.VIEWPORT_DETAIL_NUMBER);
                    if (param != null)
                    {
                        param.Set("!!" + i);
                    }
                    i++;
                }
            }
            return(viewPorts);
        }
Beispiel #8
0
        /// <summary>
        /// Creates a Placeholder Sheet
        /// </summary>
        /// <param name="SheetNumber">The sheet number</param>
        /// <param name="SheetTitle">The sheet title</param>
        /// <param name="document">Document to create the sheet in.</param>
        /// <returns name="Sheet">Returns the created placeholder sheet as a dynamo wrapped sheet.</returns>
        public static DynaSheet CreatePlaceHolderSheet(string SheetNumber, string SheetTitle,
                                                       [DefaultArgument("Synthetic.Revit.Document.Current()")] RevitDoc document)
        {
            RevitSheet revitSheet;

            if (document.IsModifiable)
            {
                TransactionManager.Instance.EnsureInTransaction(document);
                revitSheet             = RevitSheet.CreatePlaceholder(document);
                revitSheet.SheetNumber = SheetNumber;
                RevitDB.Parameter paramName = revitSheet.get_Parameter(RevitDB.BuiltInParameter.SHEET_NAME);
                paramName.Set(SheetTitle);
                TransactionManager.Instance.TransactionTaskDone();
            }
            else
            {
                using (Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(document))
                {
                    trans.Start("Create Placeholder Sheet");
                    revitSheet             = RevitSheet.CreatePlaceholder(document);
                    revitSheet.SheetNumber = SheetNumber;
                    RevitDB.Parameter paramName = revitSheet.get_Parameter(RevitDB.BuiltInParameter.SHEET_NAME);
                    paramName.Set(SheetTitle);
                    trans.Commit();
                }
            }


            return((DynaSheet)revitSheet.ToDSType(false));
        }
        public static void SetPipeDiameter(Pipe pipe, double diameter)
        {
            RevitLibrary.Parameter parameter = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM);
            Console.Write(parameter.DisplayUnitType);
            double value = UnitUtils.Convert(diameter, DisplayUnitType.DUT_MILLIMETERS, parameter.DisplayUnitType);

            parameter.Set(value);
        }
Beispiel #10
0
        /// <summary>
        /// Initialize a Wall element
        /// </summary>
        /// <param name="curve"></param>
        /// <param name="wallType"></param>
        /// <param name="baseLevel"></param>
        /// <param name="height"></param>
        /// <param name="offset"></param>
        /// <param name="flip"></param>
        /// <param name="isStructural"></param>
        private void InitWall(Curve curve, Autodesk.Revit.DB.WallType wallType, Autodesk.Revit.DB.Level baseLevel, double height, double offset, bool flip, bool isStructural)
        {
            // This creates a new wall and deletes the old one
            TransactionManager.Instance.EnsureInTransaction(Document);

            //Phase 1 - Check to see if the object exists and should be rebound
            var wallElem =
                ElementBinder.GetElementFromTrace <Autodesk.Revit.DB.Wall>(Document);

            bool successfullyUsedExistingWall = false;

            //There was a modelcurve, try and set sketch plane
            // if you can't, rebuild
            if (wallElem != null && wallElem.Location is Autodesk.Revit.DB.LocationCurve)
            {
                var wallLocation = wallElem.Location as Autodesk.Revit.DB.LocationCurve;
                if ((wallLocation.Curve is Autodesk.Revit.DB.Line == curve is Autodesk.Revit.DB.Line) ||
                    (wallLocation.Curve is Autodesk.Revit.DB.Arc == curve is Autodesk.Revit.DB.Arc) ||
                    (wallLocation.Curve is Autodesk.Revit.DB.Ellipse == curve is Autodesk.Revit.DB.Ellipse))
                {
                    if (!CurveUtils.CurvesAreSimilar(wallLocation.Curve, curve))
                    {
                        wallLocation.Curve = curve;
                    }

                    Autodesk.Revit.DB.Parameter baseLevelParameter =
                        wallElem.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.WALL_BASE_CONSTRAINT);
                    Autodesk.Revit.DB.Parameter topOffsetParameter =
                        wallElem.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.WALL_USER_HEIGHT_PARAM);
                    Autodesk.Revit.DB.Parameter wallTypeParameter =
                        wallElem.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.ELEM_TYPE_PARAM);
                    if (baseLevelParameter.AsElementId() != baseLevel.Id)
                    {
                        baseLevelParameter.Set(baseLevel.Id);
                    }
                    if (Math.Abs(topOffsetParameter.AsDouble() - height) > 1.0e-10)
                    {
                        topOffsetParameter.Set(height);
                    }
                    if (wallTypeParameter.AsElementId() != wallType.Id)
                    {
                        wallTypeParameter.Set(wallType.Id);
                    }
                    successfullyUsedExistingWall = true;
                }
            }

            var wall = successfullyUsedExistingWall ? wallElem :
                       Autodesk.Revit.DB.Wall.Create(Document, curve, wallType.Id, baseLevel.Id, height, offset, flip, isStructural);

            InternalSetWall(wall);

            TransactionManager.Instance.TransactionTaskDone();

            // delete the element stored in trace and add this new one
            ElementBinder.CleanupAndSetElementForTrace(Document, InternalWall);
        }
Beispiel #11
0
        private void SetParameterValue(Autodesk.Revit.DB.Parameter param, int value)
        {
            if (param.StorageType != StorageType.Integer && param.StorageType != StorageType.Double)
            {
                throw new Exception("The parameter's storage type is not a number.");
            }

            param.Set(value);
        }
Beispiel #12
0
        private void SetParameterValue(Autodesk.Revit.DB.Parameter param, Revit.Elements.Element value)
        {
            if (param.StorageType != StorageType.ElementId)
            {
                throw new Exception("The parameter's storage type is not an Element.");
            }

            param.Set(value.InternalElementId);
        }
Beispiel #13
0
        public static void SetParameterValue(Autodesk.Revit.DB.Parameter param, string value)
        {
            if (param.StorageType != StorageType.String)
            {
                throw new Exception(Properties.Resources.ParameterStorageNotString);
            }

            param.Set(value);
        }
Beispiel #14
0
        private void SetParameterValue(Autodesk.Revit.DB.Parameter param, string value)
        {
            if (param.StorageType != StorageType.String)
            {
                throw new Exception("The parameter's storage type is not a string.");
            }

            param.Set(value);
        }
Beispiel #15
0
        public static void SetParameterValue(Autodesk.Revit.DB.Parameter param, bool value)
        {
            if (param.StorageType != StorageType.Integer)
            {
                throw new Exception(Properties.Resources.ParameterStorageNotInteger);
            }

            param.Set(value == false ? 0 : 1);
        }
Beispiel #16
0
        private void SetParameterValue(Autodesk.Revit.DB.Parameter param, SIUnit value)
        {
            if (param.StorageType != StorageType.Double)
            {
                throw new Exception("The parameter's storage type is not an integer.");
            }

            param.Set(value.ConvertToHostUnits());
        }
Beispiel #17
0
        public static void SetParameterValue(Autodesk.Revit.DB.Parameter param, Element value)
        {
            if (param.StorageType != StorageType.ElementId)
            {
                throw new Exception(Properties.Resources.ParameterStorageNotElement);
            }

            param.Set(value.InternalElement.Id);
        }
Beispiel #18
0
        private void SetParameterValue(Autodesk.Revit.DB.Parameter param, bool value)
        {
            if (param.StorageType != StorageType.Integer)
            {
                throw new Exception("The parameter's storage type is not an integer.");
            }

            param.Set(value == false ? 0 : 1);
        }
Beispiel #19
0
        public static void SetParameterValue(Autodesk.Revit.DB.Parameter param, int value)
        {
            if (param.StorageType != StorageType.Integer && param.StorageType != StorageType.Double)
            {
                throw new Exception(Properties.Resources.ParameterStorageNotNumber);
            }

            var valueToSet = value * UnitConverter.DynamoToHostFactor(param.Definition.UnitType);

            param.Set(valueToSet);
        }
Beispiel #20
0
        private static void SetParameterValue(Autodesk.Revit.DB.Parameter param, int value)
        {
            if (param.StorageType != StorageType.Integer && param.StorageType != StorageType.Double)
            {
                throw new Exception("The parameter's storage type is not a number.");
            }

            var valueToSet = GetConvertedParameterValue(param, value);

            param.Set(valueToSet);
        }
Beispiel #21
0
        private static void SetParameterValue(Autodesk.Revit.DB.Parameter param, int value)
        {
            if (param.StorageType != StorageType.Integer && param.StorageType != StorageType.Double)
            {
                throw new Exception(Properties.Resources.ParameterStorageNotNumber);
            }

            var valueToSet = GetConvertedParameterValue(param, value);

            param.Set(valueToSet);
        }
Beispiel #22
0
        //----------------------------------------------------------
        public static void GetParameterValue(Parameter para, Parameter para_change)
        {
            try
            {
                if (para != null)
                {
                    if (para.Definition.Name == para_change.Definition.Name && para.IsReadOnly == false)
                    {
                        // Use different method to get parameter data according to the storage type
                        switch (para.StorageType)
                        {
                        case StorageType.Double:
                            //covert the number into Metric
                            para.Set(para_change.AsDouble());
                            break;

                        case StorageType.ElementId:
                            //find out the name of the element
                            para.Set(para_change.AsElementId());
                            break;

                        case StorageType.Integer:
                            para.Set(para_change.AsInteger());
                            break;

                        case StorageType.String:
                            para.Set(para_change.AsString());
                            break;

                        default:
                            //defValue = "Unexposed parameter.";
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Beispiel #23
0
        private void WriteOnParam(string paramId, Document doc, string value)
        {
            ProjectInfo projectInfoElement = doc.ProjectInformation;

            IList <Autodesk.Revit.DB.Parameter> parameters = projectInfoElement.GetParameters(paramId);

            if (parameters.Count != 0)
            {
                Autodesk.Revit.DB.Parameter p = parameters.FirstOrDefault();
                if (!p.IsReadOnly)
                {
                    p.Set(value);
                }
            }
        }
        public static bool setParameterValueString(Element curElem, string paramName, string newValue)
        {
            bool functionReturnValue = false;

            Autodesk.Revit.DB.Parameter curParam = curElem.GetParameters(paramName).First();

            try {
                curParam.Set(newValue);
                return(true);
            } catch (Exception ex) {
                //error
                TaskDialog.Show("Error", "Could not change parameter value");
            }

            return(false);
        }
        public static void PanelUpdate(Document doc, UIDocument uidoc, List <string> panFilter, Dictionary <string, Panel> panList, List <Element> assemblies, bool useParameter, string parameterName)
        {
            if (useParameter == false)
            {
                foreach (AssemblyInstance a in assemblies)
                {
                    if (panFilter.Contains(a.Name))
                    {
                        foreach (KeyValuePair <string, string> entry in panList[a.Name].Parameters)
                        {
                            try
                            {
                                Autodesk.Revit.DB.Parameter p = a.LookupParameter(entry.Key);

                                p.Set(entry.Value);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (AssemblyInstance a in assemblies)
                {
                    string pName = a.LookupParameter(parameterName).AsString();
                    if (panFilter.Contains(pName))
                    {
                        foreach (KeyValuePair <string, string> entry in panList[pName].Parameters)
                        {
                            try
                            {
                                Autodesk.Revit.DB.Parameter p = a.LookupParameter(entry.Key);

                                p.Set(entry.Value);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }
        }
Beispiel #26
0
        /// <summary>
        /// Depending on SerialElementId properties, method attempts to find the Element referenced by the ElementId by first trying UniqueId, then Id, then Name, then Aliases of the name.  If an element is found, then set the parameter to the Element's Id.
        /// </summary>
        /// <param name="param">Parameter to modify</param>
        /// <param name="serialElementId">A SerialElementId that references an element in the Document.</param>
        /// <param name="document">A revit document</param>
        /// <returns name="status">Returns true if parameter was successfully set and false if not.  Parameter retains it previous value if Set was unsucessful.</returns>
        private static bool _ModifyElementIdParameter(RevitParam param, SerialElementId serialElementId, RevitDoc document)
        {
            //  Initialize variables.
            // Status of parameter change.
            bool status = false;

            //  Element to set the ElementId parameter.
            //  Will return null if element is not found.
            RevitElem elem = serialElementId.GetElem(document);

            //  If elem was selected, then set the parameter to that element.
            if (elem != null)
            {
                status = param.Set(elem.Id);
            }

            // Return if parameter change was successful.
            return(status);
        }
Beispiel #27
0
        /// <summary>
        /// Given a list of viewports, grid spacing and an origin point, function will renumber the viewports based on grid location.
        /// </summary>
        /// <param name="viewports">Revit ViewPorts</param>
        /// <param name="gridX">Grid spacing in the X direction</param>
        /// <param name="gridY">Grid spacing in the Y direction</param>
        /// <param name="originX">X coordinate of the grid origin</param>
        /// <param name="originY">Y coordinate of the grid origin</param>
        /// <returns name="viewports">The renumbered Revit ViewPorts</returns>
        internal static List <revitViewport> _renumberViewports(List <revitViewport> viewports, double gridX, double gridY, double originX, double originY)
        {
            //const double viewportOffset = 0.0114;

            //int i = 1;

            foreach (revitViewport vp in viewports)
            {
                revitOutline labelOutline = vp.GetLabelOutline();
                revitXYZ     minPt        = labelOutline.MinimumPoint;

                string viewNumber = _calculateViewNumber(minPt.X, minPt.Y, gridX, gridY, originX, originY);

                revitDB.Parameter param = vp.get_Parameter(revitDB.BuiltInParameter.VIEWPORT_DETAIL_NUMBER);
                if (param != null)
                {
                    param.Set(viewNumber);
                }

                //double x = Math.Floor(((minPt.X - originX) + viewportOffset) / gridX + 1);
                //double y = Math.Floor(((minPt.Y - originY) + viewportOffset) / gridY + 1);

                //if (x > 0 && y > 0)
                //{
                //    char yChar = (char)('A' - 1 + y);
                //    string yString = yChar.ToString();

                //    vp.get_Parameter(revitDB.BuiltInParameter.VIEWPORT_DETAIL_NUMBER).Set(yString + x);
                //}
                //else
                //{
                //    vp.get_Parameter(revitDB.BuiltInParameter.VIEWPORT_DETAIL_NUMBER).Set("!!" + i + "!!");
                //}
            }

            return(viewports);
        }
 internal static bool SetDoubleInRhinoUnits(this DB.Parameter parameter, double value)
 {
     return(parameter.Set(UnitConverter.InHostUnits(value, parameter.Definition.ParameterType)));
 }
        internal static bool SetParameter(IGH_ActiveObject obj, DB.Parameter parameter, IGH_Goo goo)
        {
            if (goo is null)
            {
                return(false);
            }

            try
            {
                var element  = parameter.Element;
                var value    = goo.ScriptVariable();
                var document = default(DB.Document);
                if (value is DB.Parameter paramValue)
                {
                    switch (paramValue.StorageType)
                    {
                    case DB.StorageType.Integer: value = paramValue.AsInteger(); break;

                    case DB.StorageType.Double: value = paramValue.AsDoubleInRhinoUnits(); break;

                    case DB.StorageType.String: value = paramValue.AsString(); break;

                    case DB.StorageType.ElementId: value = paramValue.AsElementId(); document = paramValue.Element.Document; break;
                    }
                }

                switch (parameter.StorageType)
                {
                case DB.StorageType.Integer:
                {
                    switch (value)
                    {
                    case bool boolean: parameter.Set(boolean ? 1 : 0); break;

                    case int integer: parameter.Set(integer); break;

                    case double real: parameter.SetDoubleInRhinoUnits((int)Clamp(Round(real), int.MinValue, int.MaxValue)); break;

                    case System.Drawing.Color color: parameter.Set(((int)color.R) | ((int)color.G << 8) | ((int)color.B << 16)); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.Double:
                {
                    switch (value)
                    {
                    case int integer: parameter.Set((double)integer); break;

                    case double real: parameter.SetDoubleInRhinoUnits(real); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.String:
                {
                    switch (value)
                    {
                    case string str: parameter.Set(str); break;

                    default: element = null; break;
                    }
                    break;
                }

                case DB.StorageType.ElementId:
                {
                    switch (value)
                    {
                    case DB.Element ele:
                        if (element.Document.Equals(ele.Document))
                        {
                            parameter.Set(ele.Id);
                        }
                        else
                        {
                            obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Referencing elements from other documents is not valid");
                        }
                        break;

                    case DB.Category cat:
                        if (element.Document.Equals(cat.Document()))
                        {
                            parameter.Set(cat.Id);
                        }
                        else
                        {
                            obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Referencing categories from other documents is not valid");
                        }
                        break;

                    case DB.ElementId id:
                        if (document is object)
                        {
                            if (element.Document.Equals(document))
                            {
                                parameter.Set(id);
                            }
                            else
                            {
                                obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Referencing elements from other documents is not valid");
                            }
                        }
                        else
                        {
                            element = null;
                        }
                        break;

                    default: element = null; break;
                    }
                    break;
                }

                default:
                {
                    element = null;
                    break;
                }
                }

                if (element is null && parameter is object)
                {
                    obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Failed to cast from {value.GetType().Name} to {parameter.StorageType.ToString()}.");
                    return(false);
                }
            }
            catch (Autodesk.Revit.Exceptions.InvalidOperationException e)
            {
                obj.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Failed to set '{parameter.Definition.Name}' value : {e.Message}");
                return(false);
            }

            return(true);
        }
Beispiel #30
0
        public static void SetValue(this Autodesk.Revit.DB.Parameter p, object obj)
        {
            var tx = revitData.Transaction;

            if (tx == null)
            {
                throw new Exception("Chưa khởi tạo Transaction");
            }
            if (tx.GetStatus() != TransactionStatus.Started)
            {
                throw new Exception("Transaction chưa bắt đầu.");
            }
            if (p.IsReadOnly)
            {
                throw new Exception("Parameter không được phép điều chỉnh.");
            }
            if (p == null)
            {
                throw new Exception("Parameter không tồn tại");
            }

            var invalidTypeError = "Giá trị không đúng kiểu dữ liệu";

            switch (p.StorageType)
            {
            case StorageType.Double:
                if (!(obj is Double))
                {
                    throw new Exception(invalidTypeError);
                }
                p.Set((double)obj);
                break;

            case StorageType.String:
                if (!(obj is String))
                {
                    throw new Exception(invalidTypeError);
                }
                p.Set((String)obj);
                break;

            case StorageType.Integer:
                if (!(obj is Int32))
                {
                    throw new Exception(invalidTypeError);
                }
                p.Set((Int32)obj);
                break;

            case StorageType.ElementId:
                if (obj is Autodesk.Revit.DB.Element)
                {
                    p.Set(((Autodesk.Revit.DB.Element)obj).Id);
                }
                else if (obj is Autodesk.Revit.DB.ElementId)
                {
                    p.Set((Autodesk.Revit.DB.ElementId)obj);
                }
                else
                {
                    throw new Exception(invalidTypeError);
                }
                break;
            }
        }