Example #1
0
        public override object NewElement(object parent)
        {
            FamilyComponent component = null;

            System.Windows.Forms.OpenFileDialog dialogue = new System.Windows.Forms.OpenFileDialog();
            dialogue.Filter           = "Revit Families (*.rfa)|*.rfa|All files (*.*)|*.*";
            dialogue.RestoreDirectory = true;
            if (dialogue.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileInfo file = new FileInfo(dialogue.FileName);
                Autodesk.Revit.DB.Document doc = ((Autodesk.Revit.ApplicationServices.Application)ADSKApplciation).OpenDocumentFile(file.FullName);

                component              = FamilyComponent.NewFamilyComponent(SQLiteConnection, ComponentDataView, ActiveUser, ADSKApplciation, FamilyComponentTypeItems.InternalCollection[0]);
                component.FileName     = file.Name;
                component.FileSize     = file.Length;
                component.DateCreated  = DateTime.Now;
                component.DateModified = DateTime.Now;
                component.IsReleased   = false;
                component.State        = EntityStates.Enabled;

                byte[] image = Utils.ThumbnailFromView(doc, "Thumbnail");
                if (image == null)
                {
                    component.Thumbnail = new byte[byte.MaxValue];
                }
                else
                {
                    component.Thumbnail = image;
                }

                if (doc.OwnerFamily.FamilyCategory != null)
                {
                    component.FamilyCategory = doc.OwnerFamily.FamilyCategory.Name;
                }
                else
                {
                    component.FamilyCategory = "None";
                }

                Autodesk.Revit.DB.ElementId alwaysVerticalId            = new Autodesk.Revit.DB.ElementId(Autodesk.Revit.DB.BuiltInParameter.FAMILY_ALWAYS_VERTICAL);
                Autodesk.Revit.DB.ElementId canHostRebarlId             = new Autodesk.Revit.DB.ElementId(Autodesk.Revit.DB.BuiltInParameter.FAMILY_CAN_HOST_REBAR);
                Autodesk.Revit.DB.ElementId cutsWalllId                 = new Autodesk.Revit.DB.ElementId(Autodesk.Revit.DB.BuiltInParameter.FAMILY_ALLOW_CUT_WITH_VOIDS);
                Autodesk.Revit.DB.ElementId sharedId                    = new Autodesk.Revit.DB.ElementId(Autodesk.Revit.DB.BuiltInParameter.FAMILY_SHARED);
                Autodesk.Revit.DB.ElementId OmniClassCodeId             = new Autodesk.Revit.DB.ElementId(Autodesk.Revit.DB.BuiltInParameter.OMNICLASS_CODE);
                Autodesk.Revit.DB.ElementId omniClassNameId             = new Autodesk.Revit.DB.ElementId(Autodesk.Revit.DB.BuiltInParameter.OMNICLASS_DESCRIPTION);
                Autodesk.Revit.DB.ElementId parttypeId                  = new Autodesk.Revit.DB.ElementId(Autodesk.Revit.DB.BuiltInParameter.FAMILY_CONTENT_PART_TYPE);
                Autodesk.Revit.DB.ElementId roomCalculationpointId      = new Autodesk.Revit.DB.ElementId(Autodesk.Revit.DB.BuiltInParameter.ROOM_CALCULATION_POINT);
                Autodesk.Revit.DB.ElementId roundCOnnectorDescriptionId = new Autodesk.Revit.DB.ElementId(Autodesk.Revit.DB.BuiltInParameter.FAMILY_ROUNDCONNECTOR_DIMENSIONTYPE);
                Autodesk.Revit.DB.ElementId workPlaneBasedId            = new Autodesk.Revit.DB.ElementId(Autodesk.Revit.DB.BuiltInParameter.FAMILY_WORK_PLANE_BASED);

                foreach (Autodesk.Revit.DB.Parameter parameter in doc.OwnerFamily.Parameters)
                {
                    if (parameter.Id == alwaysVerticalId)
                    {
                        component.AlwaysVertical = Convert.ToBoolean(parameter.AsInteger());
                    }

                    if (parameter.Id == canHostRebarlId)
                    {
                        component.CanHostRebar = Convert.ToBoolean(parameter.AsInteger());
                    }

                    if (parameter.Id == cutsWalllId)
                    {
                        component.CutsWithVoidWhenLoaded = Convert.ToBoolean(parameter.AsInteger());
                    }

                    if (parameter.Id == sharedId)
                    {
                        component.IsShared = Convert.ToBoolean(parameter.AsInteger());
                    }

                    if (parameter.Id == omniClassNameId)
                    {
                        component.OmniClassTitle = parameter.AsString();
                    }

                    if (parameter.Id == OmniClassCodeId)
                    {
                        component.OmnoClassNumber = parameter.AsString();
                    }

                    if (parameter.Id == parttypeId)
                    {
                        component.PartType = parameter.AsValueString();
                    }

                    if (parameter.Id == roomCalculationpointId)
                    {
                        component.RoomCalculationPoint = Convert.ToBoolean(parameter.AsInteger());
                    }

                    if (parameter.Id == roundCOnnectorDescriptionId)
                    {
                        component.RoundConnectorDimension = parameter.AsValueString();
                    }

                    if (parameter.Id == workPlaneBasedId)
                    {
                        component.WorkPlaneBased = Convert.ToBoolean(parameter.AsInteger());
                    }
                }

                if (component.PartType == string.Empty)
                {
                    component.PartType = "N/A";
                }

                if (component.RoundConnectorDimension == string.Empty)
                {
                    component.PartType = "N/A";
                }

                component.EndEdit();

                if (doc.OwnerFamily.FamilyCategory != null)
                {
                    component.FamilyCategory = doc.OwnerFamily.FamilyCategory.Name;
                }
                else
                {
                    component.FamilyCategory = "None";
                }

                component.EndEdit();

                Utils.GetFamilyComponentFeatures(component, doc, ActiveUser);
                Utils.GetFamilyComponentParameters(component, doc, ActiveUser);
                Utils.GetFamilyComponentReferencePlanes(component, doc, ActiveUser);

                doc.Close(false);

                component.FamilyFile = Utils.FileToByteArray(file.FullName);
                SelectedElement      = component;
                RefreshCollections();
            }
            return(component);
        }
Example #2
0
        public static Dictionary <string, object> UpgradeFamilies(string directoryPath)
        {
            //get UIAPP and APP
            var uiapp = DocumentManager.Instance.CurrentUIApplication;
            var app   = uiapp.Application;

            //read files from directory
            string[] allfiles = System.IO.Directory.GetFiles(directoryPath, "*.rfa*", System.IO.SearchOption.AllDirectories);
            //create a new form!
            FamilyUpgradeForm statusBar = new FamilyUpgradeForm("Rhythm - Bulk Upgrade Families", "Upgrading family {0} of " + allfiles.Length.ToString(), "Batch Family Upgrayedd", allfiles.Length);
            //declare lists to output
            List <string> upgradedFiles = new List <string>();
            //List<string> notUpgradedFiles = new List<string>();

            //build a file info to see if any of the families are read only
            List <FileInfo> fileInfos = new List <FileInfo>();

            foreach (var file in allfiles)
            {
                fileInfos.Add(new FileInfo(file));
            }
            //grab the files that can be upgraded and place in new list (reduces memory consumption)
            string[] toUpgrade = new List <string>(fileInfos.Where(fi => !fi.IsReadOnly).Select(fi => fi.ToString())).ToArray();
            //add the ones that are read only to the list
            List <string> notUpgradedFiles = new List <string>(fileInfos.Where(fi => fi.IsReadOnly).Select(fi => fi.ToString()));

            //flag for read only
            bool flag = fileInfos.Any(f => f.IsReadOnly);

            //show message for Read Only
            if (flag)
            {
                TaskDialog.Show("Family Upgrade Alert",
                                "Some of the families you are trying to upgrade are read only. These will be output in the failed category. If you want to upgrade them, please change from read only and try again.");
            }

            //something to increase each time
            int step = 0;

            //loop through each file and  try to upgrade. This is important because we open and close each file on its own.
            while (step < toUpgrade.Length)
            {
                statusBar.Activate();
                try
                {
                    if (statusBar.getAbortFlag())
                    {
                        break;
                    }
                    Autodesk.Revit.DB.Document document = app.OpenDocumentFile(toUpgrade[step]);
                    document.Close(true);
                    upgradedFiles.Add(toUpgrade[step]);
                }
                catch
                {
                    Autodesk.Revit.DB.Document document = app.OpenDocumentFile(toUpgrade[step]);
                    document.Close(false);
                    notUpgradedFiles.Add(toUpgrade[step]);
                }
                ++step;
                statusBar.Increment();
            }
            statusBar.Close();
            //clean up some backup files
            string[] backupFiles = System.IO.Directory.GetFiles(directoryPath, "*.0001*", System.IO.SearchOption.AllDirectories);
            foreach (string file in backupFiles)
            {
                File.Delete(file);
            }
            //returns the outputs
            var outInfo = new Dictionary <string, object>
            {
                { "Successfully Upgraded", upgradedFiles },
                { "Not So Successfully Upgraded", notUpgradedFiles }
            };

            return(outInfo);
        }