Example #1
0
        public void saveInfoStorage(InfosStorage storage)
        {
            System.Xml.Serialization.XmlSerializer utilsSerializer =
                new System.Xml.Serialization.XmlSerializer(typeof(UtilsStorage));
            System.Xml.Serialization.XmlSerializer infoStorageSerializer =
                new System.Xml.Serialization.XmlSerializer(typeof(InfosStorage));

            UtilsStorage utilsStorage = null;

            try
            {
                using (StreamReader r = new StreamReader("C:\\TEMP\\ccsettings.xml"))
                {
                    utilsStorage = (UtilsStorage)utilsSerializer.Deserialize(r);
                }
            }
            catch (Exception) { }

            if (utilsStorage == null)
            {
                string dllPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                string folder  = System.IO.Path.GetDirectoryName(dllPath);
                utilsStorage      = new UtilsStorage();
                utilsStorage.path = folder;
            }

            System.Windows.Forms.SaveFileDialog storageDialog = new System.Windows.Forms.SaveFileDialog();
            storageDialog.InitialDirectory = utilsStorage.path;
            storageDialog.Filter           = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
            if (storageDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                xmlFilePath = storageDialog.FileName;
            }

            using (StreamWriter r = new StreamWriter(xmlFilePath))
            {
                infoStorageSerializer.Serialize(r, storage);
            }

            if (!Directory.Exists("C:\\TEMP"))
            {
                Directory.CreateDirectory("C:\\TEMP");
            }

            using (StreamWriter r = new StreamWriter("C:\\TEMP\\ccsettings.xml"))
            {
                utilsSerializer.Serialize(r, utilsStorage);
            }
        }
        private void setClassificator(Classificator classificator, InfosStorage storage, Element elem, bool check)
        {
            bool          paramChecker;
            List <string> assignedValues = new List <string>();

            if (classificator.paramsValues.Count > storage.instanseParams.Count)
            {
                Print(string.Format("Значение параметра: \"{0}\" в элементе: \"{1}\" за пределами диапазона возможных значений. Присвоение данного параметра не будет выполнено."
                                    , classificator.paramsValues[classificator.paramsValues.Count - 1]
                                    , classificator.FamilyName)
                      , KPLN_Loader.Preferences.MessageType.Warning);
            }
            for (int i = 0; i < Math.Min(classificator.paramsValues.Count, storage.instanseParams.Count); i++)
            {
                if (classificator.paramsValues[i].Length == 0)
                {
                    continue;
                }
                paramChecker = setParam(elem, storage.instanseParams[i], classificator.paramsValues[i], out string valueForAssigned);
                if (paramChecker)
                {
                    assignedValues.Add(valueForAssigned);
                }
            }
            if (assignedValues.Count == Math.Min(classificator.paramsValues.Where(i => i.Length > 0).Count(), storage.instanseParams.Where(i => i.Length > 0).Count()))
            {
                fullSuccessElems.Add(elem);
            }
            else
            {
                notFullSuccessElems.Add(elem);
            }
            if (check)
            {
                Print(string.Format("Были присвоены значения: {0}", string.Join("; ", assignedValues)), KPLN_Loader.Preferences.MessageType.System_OK);
            }
        }
        public bool startClassification(List <Element> constrs, InfosStorage storage, Document doc)
        {
            if (constrs == null || constrs.Count == 0)
            {
                Print("Не удалось получить элементы для заполнения классиифкатора!", KPLN_Loader.Preferences.MessageType.Error);
                return(false);
            }
            foreach (Classificator classificator in storage.classificator)
            {
                PrintDebug(string.Format("{0} - {1}", classificator.FamilyName, classificator.TypeName), KPLN_Loader.Preferences.MessageType.Code, debugMode);
            }
            PrintDebug("Заполнение классификатора ↑", KPLN_Loader.Preferences.MessageType.Header, debugMode);

            foreach (Element elem in constrs)
            {
                string familyName = null;
                if (elem is Room)
                {
                    familyName = elem.get_Parameter(BuiltInParameter.ROOM_DEPARTMENT).AsString();
                }
                else
                {
                    try
                    {
                        familyName = elem.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsValueString();
                    }
                    catch (Exception) { }
                    familyName = familyName == null || familyName.Length == 0 ? (elem as ElementType).FamilyName : familyName;
                }

                PrintDebug(string.Format("{0} : {1} : {2}", elem.Name, familyName, elem.Id.IntegerValue), KPLN_Loader.Preferences.MessageType.System_Regular, debugMode);
                foreach (Classificator classificator in storage.classificator)
                {
                    bool categoryCatch = false;
                    try
                    {
                        categoryCatch = Category.GetCategory(doc, classificator.BuiltInName).Id.IntegerValue == elem.Category.Id.IntegerValue;
                    }
                    catch (Exception)
                    {
                        Print(string.Format("Не удалось определить категорию: {0}. Возможно, она введена неверно.", classificator.BuiltInName), KPLN_Loader.Preferences.MessageType.Error);
                    }
                    bool familyNameCatch    = nameChecker(classificator.FamilyName, familyName);
                    bool typeNameCatch      = nameChecker(classificator.TypeName, elem.Name);
                    bool familyNameNotExist = classificator.FamilyName.Length == 0;
                    bool typeNameNotExist   = classificator.TypeName.Length == 0;

                    if (categoryCatch && familyNameCatch && typeNameCatch && !familyNameNotExist && !typeNameNotExist)
                    {
                        setClassificator(classificator, storage, elem, debugMode);
                        break;
                    }
                    if (categoryCatch && familyNameCatch && !familyNameNotExist && typeNameNotExist)
                    {
                        setClassificator(classificator, storage, elem, debugMode);
                        break;
                    }
                    if (categoryCatch && typeNameCatch && familyNameNotExist && !typeNameNotExist)
                    {
                        setClassificator(classificator, storage, elem, debugMode);
                        break;
                    }
                    if (categoryCatch && familyNameNotExist && typeNameNotExist)
                    {
                        setClassificator(classificator, storage, elem, debugMode);
                        break;
                    }
                }
            }
            return(true);
        }
Example #4
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document  doc        = commandData.Application.ActiveUIDocument.Document;
            Selection sel        = commandData.Application.ActiveUIDocument.Selection;
            View      activeView = doc.ActiveView;

            StorageUtils utils = new StorageUtils();

            ClassificatorForm form = new ClassificatorForm(utils);

            form.ShowDialog();
            if (form.DialogResult != System.Windows.Forms.DialogResult.OK)
            {
                return(Result.Cancelled);
            }

            bool debugMode = form.debugMode;

            InfosStorage storage = form.storage;

            if (storage == null)
            {
                Print("Не удалось обработать конфигурационный файл!", KPLN_Loader.Preferences.MessageType.Error);
                return(Result.Cancelled);
            }

            List <BuiltInCategory> constrCats = form.checkedCats;

            using (Transaction t = new Transaction(doc))
            {
                t.Start("Заполнение параметров классификатора");

                if (form.instanceOrType == 2)
                {
                    List <Element> constrsTypes;

                    if (sel.GetElementIds().Count == 0)
                    {
                        constrsTypes = new FilteredElementCollector(doc).WhereElementIsElementType()
                                       .WherePasses(new ElementMulticategoryFilter(constrCats))
                                       .ToElements()
                                       .Where(i => i.get_Parameter(BuiltInParameter.UNIFORMAT_CODE) != null)
                                       .ToList();
                    }
                    else
                    {
                        constrsTypes = sel.GetElementIds()
                                       .Select(elem => doc.GetElement(elem).GetTypeId())
                                       .Select(type => doc.GetElement(type))
                                       .ToHashSet()
                                       .ToList();
                    }

                    ParamUtils utilsForType = new ParamUtils(debugMode);
                    utilsForType.startClassification(constrsTypes, storage, doc);
                    Print(string.Format("Обработано элементов: {0}", utilsForType.fullSuccessElems.Count + utilsForType.notFullSuccessElems.Count), KPLN_Loader.Preferences.MessageType.Success);
                }

                else if (form.instanceOrType == 1)
                {
                    List <Element> constrsInstances;

                    if (sel.GetElementIds().Count == 0)
                    {
                        constrsInstances = new FilteredElementCollector(doc)
                                           .WhereElementIsNotElementType()
                                           .WherePasses(new ElementMulticategoryFilter(constrCats))
                                           .ToList();
                    }
                    else
                    {
                        constrsInstances = sel.GetElementIds().Select(elem => doc.GetElement(elem)).ToList();
                    }

                    ParamUtils utilsForInstanse = new ParamUtils(debugMode);
                    utilsForInstanse.startClassification(constrsInstances, storage, doc);

                    if (activeView.Name.Contains("3D"))
                    {
                        ViewUtils viewUtils = new ViewUtils(doc);

                        OverrideGraphicSettings overrideGraphic = ViewUtils.getStandartGraphicSettings(doc);
                        List <Element>          elems           = new FilteredElementCollector(doc, activeView.Id)
                                                                  .WhereElementIsNotElementType()
                                                                  .ToElements()
                                                                  .ToList();

                        foreach (Element elem in elems)
                        {
                            if (elem is Group)
                            {
                                continue;
                            }
                            try
                            {
                                activeView.SetElementOverrides(elem.Id, overrideGraphic);
                            }
                            catch { }
                        }
                        foreach (Element elem in utilsForInstanse.fullSuccessElems)
                        {
                            if (elem is Group)
                            {
                                continue;
                            }
                            try
                            {
                                activeView.SetElementOverrides(elem.Id, viewUtils.fullSuccessSet);
                            }
                            catch { }
                        }
                        foreach (Element elem in utilsForInstanse.notFullSuccessElems)
                        {
                            if (elem is Group)
                            {
                                continue;
                            }
                            try
                            {
                                activeView.SetElementOverrides(elem.Id, viewUtils.notFullSuccessSet);
                            }
                            catch { }
                        }
                    }
                    Print(string.Format("Обработано элементов: {0}", utilsForInstanse.fullSuccessElems.Count + utilsForInstanse.notFullSuccessElems.Count), KPLN_Loader.Preferences.MessageType.Success);
                }
                else
                {
                    Print("Выбрана некорректная операция! Проверьте конфигурационный файл.", KPLN_Loader.Preferences.MessageType.Success);
                }
                t.Commit();
            }
            return(Result.Succeeded);
        }
Example #5
0
        public InfosStorage getInfoStorage()
        {
            System.Xml.Serialization.XmlSerializer storageSerializer =
                new System.Xml.Serialization.XmlSerializer(typeof(UtilsStorage));

            UtilsStorage utilsStorage = null;

            try
            {
                using (StreamReader r = new StreamReader("C:\\TEMP\\ccsettings.xml"))
                {
                    utilsStorage = (UtilsStorage)storageSerializer.Deserialize(r);
                }
                System.Windows.Forms.OpenFileDialog storageDialog = new System.Windows.Forms.OpenFileDialog();
                storageDialog.InitialDirectory = utilsStorage.path;
                storageDialog.Multiselect      = false;
                storageDialog.Filter           = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
                if (storageDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    xmlFilePath = storageDialog.FileName;
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception) { }

            if (utilsStorage == null)
            {
                string dllPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                string folder  = System.IO.Path.GetDirectoryName(dllPath);
                System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
                dialog.InitialDirectory = folder;
                dialog.Multiselect      = false;
                dialog.Filter           = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
                if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    return(null);
                }
                xmlFilePath  = dialog.FileName;
                utilsStorage = new UtilsStorage();
            }
            utilsStorage.path = Path.GetDirectoryName(xmlFilePath);

            InfosStorage storage = new InfosStorage();

            System.Xml.Serialization.XmlSerializer serializer =
                new System.Xml.Serialization.XmlSerializer(typeof(InfosStorage));

            using (StreamReader r = new StreamReader(xmlFilePath))
            {
                storage = (InfosStorage)serializer.Deserialize(r);
            }

            if (!Directory.Exists("C:\\TEMP"))
            {
                Directory.CreateDirectory("C:\\TEMP");
            }

            using (StreamWriter r = new StreamWriter("C:\\TEMP\\ccsettings.xml"))
            {
                storageSerializer.Serialize(r, utilsStorage);
            }

            return(storage);
        }