示例#1
0
        public bool GetTheTypes(ExternalCommandData commandData, String _selectedWallTypeName)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;

            Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
            Document  doc       = uidoc.Document;
            Selection selection = uidoc.Selection;

            //read the settings file
            cmdSettingsReadWrite cls  = new cmdSettingsReadWrite();
            string _sourceProjectPath = cls.GetSetting("<GETTER_SOURCE_PROJECT>");

            //open source project
            Document _openedSourceDoc = app.OpenDocumentFile(_sourceProjectPath);


            WallType _selectedWallType = null;

            //get the wall types in the source doc
            FilteredElementCollector sourceWallTypes = new FilteredElementCollector(_openedSourceDoc).OfClass(typeof(WallType));

            foreach (WallType wt in sourceWallTypes)
            {
                if (wt.Name == _selectedWallTypeName)
                {
                    //the name matches
                    _selectedWallType = wt;
                }
            }


            //Create a wall type in the current document

            using (Transaction tx = new Transaction(doc, "Get Types"))
            {
                tx.Start("Transfer Wall Type");

                WallType newWallType = null;

                //get the wall types in the current doc
                FilteredElementCollector wallTypes = new FilteredElementCollector(doc).OfClass(typeof(WallType));

                foreach (WallType wt in wallTypes)
                {
                    if (wt.Kind == _selectedWallType.Kind)
                    {
                        newWallType = wt.Duplicate(_selectedWallTypeName) as WallType;

                        TaskDialog.Show("Wall Created", "The selected wall has been created.");

                        break;
                    }
                }


                // Assign parameter values from source wall type:

#if COPY_INDIVIDUAL_PARAMETER_VALUE
                // Example: individually copy the "Function" parameter value:

                BuiltInParameter bip      = BuiltInParameter.FUNCTION_PARAM;
                string           function = wallType.get_Parameter(bip).AsString();
                Parameter        p        = newWallType.get_Parameter(bip);
                p.Set(function);


                BuiltInParameter bip            = BuiltInParameter.WALL_ATTR_WIDTH_PARAM;
                Double           _selectedWidth = _selectedWallType.get_Parameter(bip).AsDouble();
                Parameter        newWallWidth   = newWallType.get_Parameter(bip);
                newWallWidth.Set(_selectedWidth);
#endif // COPY_INDIVIDUAL_PARAMETER_VALUE



                Parameter p = null;

                foreach (Parameter p2 in newWallType.Parameters)
                {
                    Definition d = p2.Definition;

                    if (p2.IsReadOnly)
                    {
                        System.Diagnostics.Debug.Print(string.Format("Parameter '{0}' is read-only.", d.Name));
                    }
                    else
                    {
                        p = newWallType.get_Parameter(d);

                        if (null == p)
                        {
                            System.Diagnostics.Debug.Print(string.Format("Parameter '{0}' not found on source wall type.", d.Name));
                        }
                        else
                        {
                            if (p.StorageType == StorageType.ElementId)
                            {
                                // Here you have to find the corresponding
                                // element in the target document.

                                System.Diagnostics.Debug.Print(string.Format("Parameter '{0}' is an element id.", d.Name));
                            }
                            else
                            {
                                if (p.StorageType == StorageType.String)
                                {
                                    p2.Set(p.AsString());
                                }
                                else if (p.StorageType == StorageType.Double)
                                {
                                    p2.Set(p.AsDouble());
                                }
                                else if (p.StorageType == StorageType.Integer)
                                {
                                    p2.Set(p.AsInteger());
                                }
                                System.Diagnostics.Debug.Print(string.Format("Parameter '{0}' copied.", d.Name));
                            }
                        }
                    }
                }

                TaskDialog.Show("Parameter Set", "The walls parameters have been set.");

                MemberInfo[] memberInfos = newWallType.GetType().GetMembers(BindingFlags.GetProperty);

                foreach (MemberInfo m in memberInfos)
                {
                    // Copy the writable property values here.
                    // As there are no property writable for
                    // Walltype, I ignore this process here.
                }

                tx.Commit();
                tx.Dispose();
            }

            return(true);
        }
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;

            // Open source project

            Document docHasFamily = app.OpenDocumentFile(
                _source_project_path);

            // Find system family to copy, e.g. using a named wall type

            WallType wallType = null;

            //WallTypeSet wallTypes = docHasFamily.WallTypes; // 2013

            FilteredElementCollector wallTypes
                = new FilteredElementCollector(docHasFamily) // 2014
                  .OfClass(typeof(WallType));

            int i = 0;

            foreach (WallType wt in wallTypes)
            {
                string name = wt.Name;

                Debug.Print("  {0} {1}", ++i, name);

                if (name.Equals(_wall_type_name))
                {
                    wallType = wt;
                    break;
                }
            }

            if (null == wallType)
            {
                message = string.Format(
                    "Cannot find source wall type '{0}'"
                    + " in source document '{1}'. ",
                    _wall_type_name, _source_project_path);

                return(Result.Failed);
            }

            // Create a new wall type in current document

            using (Transaction t = new Transaction(doc))
            {
                t.Start("Transfer Wall Type");

                WallType newWallType = null;

                //WallTypeSet wallTypes = doc.WallTypes; // 2013

                wallTypes = new FilteredElementCollector(doc)
                            .OfClass(typeof(WallType)); // 2014

                foreach (WallType wt in wallTypes)
                {
                    if (wt.Kind == wallType.Kind)
                    {
                        newWallType = wt.Duplicate(_wall_type_name)
                                      as WallType;

                        Debug.Print(string.Format(
                                        "New wall type '{0}' created.",
                                        _wall_type_name));

                        break;
                    }
                }

                // Assign parameter values from source wall type:

#if COPY_INDIVIDUAL_PARAMETER_VALUE
                // Example: individually copy the "Function" parameter value:

                BuiltInParameter bip      = BuiltInParameter.FUNCTION_PARAM;
                string           function = wallType.get_Parameter(bip).AsString();
                Parameter        p        = newWallType.get_Parameter(bip);
                p.Set(function);
#endif // COPY_INDIVIDUAL_PARAMETER_VALUE

                Parameter p = null;

                foreach (Parameter p2 in newWallType.Parameters)
                {
                    Definition d = p2.Definition;

                    if (p2.IsReadOnly)
                    {
                        Debug.Print(string.Format(
                                        "Parameter '{0}' is read-only.", d.Name));
                    }
                    else
                    {
                        p = wallType.get_Parameter(d);

                        if (null == p)
                        {
                            Debug.Print(string.Format(
                                            "Parameter '{0}' not found on source wall type.",
                                            d.Name));
                        }
                        else
                        {
                            if (p.StorageType == StorageType.ElementId)
                            {
                                // Here you have to find the corresponding
                                // element in the target document.

                                Debug.Print(string.Format(
                                                "Parameter '{0}' is an element id.",
                                                d.Name));
                            }
                            else
                            {
                                if (p.StorageType == StorageType.Double)
                                {
                                    p2.Set(p.AsDouble());
                                }
                                else if (p.StorageType == StorageType.String)
                                {
                                    p2.Set(p.AsString());
                                }
                                else if (p.StorageType == StorageType.Integer)
                                {
                                    p2.Set(p.AsInteger());
                                }
                                Debug.Print(string.Format(
                                                "Parameter '{0}' copied.", d.Name));
                            }
                        }
                    }

                    // Note:
                    // If a shared parameter parameter is attached,
                    // you need to create the shared parameter first,
                    // then copy the parameter value.
                }

                // If the system family type has some other properties,
                // you need to copy them as well here. Reflection can
                // be used to determine the available properties.

                MemberInfo[] memberInfos = newWallType.GetType()
                                           .GetMembers(BindingFlags.GetProperty);

                foreach (MemberInfo m in memberInfos)
                {
                    // Copy the writable property values here.
                    // As there are no property writable for
                    // Walltype, I ignore this process here.
                }
                t.Commit();
            }
            return(Result.Succeeded);
        }
示例#3
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;


            // Open source project
            Document docHasFamily = app.OpenDocumentFile(_source_project_path);

            // Find system family to copy, e.g. using a named wall type
            WallType _selectedWallType = null;

            FilteredElementCollector wallTypes
                = new FilteredElementCollector(docHasFamily) // 2014
                  .OfClass(typeof(WallType));

            int i = 0;



            foreach (WallType wt in wallTypes)
            {
                //Add the wall type to the list
                m_data.Add(wt.Name);
            }

            //show the form and get a value back from it
            //See this page for example: https://stackoverflow.com/questions/5233502/how-to-return-a-value-from-a-form-in-c
            using (var f = new frmListDialog(m_data))
            {
                var result = f.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    _selectedWallTypeName = f.ReturnValue1; //values preserved after close

                    //loop thru and find the wall type the matches the name
                    foreach (WallType wt in wallTypes)
                    {
                        if (wt.Name == _selectedWallTypeName)
                        {
                            //if its a match then...
                            _selectedWallType = wt;
                            //_selectedWallTypeName = wt.Name;
                        }
                    }
                }
            }



            //foreach( WallType wt in wallTypes )
            //{
            //  string name = wt.Name;

            //  Debug.Print( "  {0} {1}", ++i, name );

            //  if( name.Equals( _selectedWallTypeName ) )
            //  {
            //    _selectedWallType = wt;
            //    break;
            //  }
            //}

            //if( null == _selectedWallType )
            //{
            //  message = string.Format(
            //    "Cannot find source wall type '{0}'"
            //    + " in source document '{1}'. ",
            //    _selectedWallTypeName, _source_project_path );

            //  return Result.Failed;
            //}



            // Create a new wall type in current document

            using (Transaction t = new Transaction(doc))
            {
                t.Start("Transfer Wall Type");

                WallType newWallType = null;

                wallTypes = new FilteredElementCollector(doc).OfClass(typeof(WallType)); // 2014

                foreach (WallType wt in wallTypes)
                {
                    if (wt.Kind == _selectedWallType.Kind)
                    {
                        newWallType = wt.Duplicate(_selectedWallTypeName) as WallType;

                        TaskDialog.Show("New wall type '{0}' created.", "Wall type " + _selectedWallTypeName + "created.");

                        break;
                    }
                }

                // Assign parameter values from source wall type:

#if COPY_INDIVIDUAL_PARAMETER_VALUE
                // Example: individually copy the "Function" parameter value:

                BuiltInParameter bip      = BuiltInParameter.FUNCTION_PARAM;
                string           function = wallType.get_Parameter(bip).AsString();
                Parameter        p        = newWallType.get_Parameter(bip);
                p.Set(function);
#endif // COPY_INDIVIDUAL_PARAMETER_VALUE

                Parameter p = null;

                foreach (Parameter p2 in newWallType.Parameters)
                {
                    Definition d = p2.Definition;

                    if (p2.IsReadOnly)
                    {
                        Debug.Print(string.Format(
                                        "Parameter '{0}' is read-only.", d.Name));
                    }
                    else
                    {
                        p = _selectedWallType.get_Parameter(d);

                        if (null == p)
                        {
                            Debug.Print(string.Format(
                                            "Parameter '{0}' not found on source wall type.",
                                            d.Name));
                        }
                        else
                        {
                            if (p.StorageType == StorageType.ElementId)
                            {
                                // Here you have to find the corresponding
                                // element in the target document.

                                Debug.Print(string.Format(
                                                "Parameter '{0}' is an element id.",
                                                d.Name));
                            }
                            else
                            {
                                if (p.StorageType == StorageType.Double)
                                {
                                    p2.Set(p.AsDouble());
                                }
                                else if (p.StorageType == StorageType.String)
                                {
                                    p2.Set(p.AsString());
                                }
                                else if (p.StorageType == StorageType.Integer)
                                {
                                    p2.Set(p.AsInteger());
                                }


                                Debug.Print(string.Format(
                                                "Parameter '{0}' copied.", d.Name));
                            }
                        }
                    }

                    // Note:
                    // If a shared parameter parameter is attached,
                    // you need to create the shared parameter first,
                    // then copy the parameter value.
                }

                // If the system family type has some other properties,
                // you need to copy them as well here. Reflection can
                // be used to determine the available properties.

                MemberInfo[] memberInfos = newWallType.GetType()
                                           .GetMembers(BindingFlags.GetProperty);

                foreach (MemberInfo m in memberInfos)
                {
                    // Copy the writable property values here.
                    // As there are no property writable for
                    // Walltype, I ignore this process here.
                }
                t.Commit();
            }
            return(Result.Succeeded);
        }