Exemplo n.º 1
0
        private string ChooseFile(string format)
        {
            FileOpenDialog openFileDialog = new FileOpenDialog(format);

            openFileDialog.Show();
            ModelPath userPath = openFileDialog.GetSelectedModelPath();

            ModelPathUtils.ConvertModelPathToUserVisiblePath(userPath);
            return(ModelPathUtils.ConvertModelPathToUserVisiblePath(userPath));
        }
Exemplo n.º 2
0
        public void Execute(UIApplication app)
        {
            Transaction transaction = null;

            try
            {
                this.calculator = new SimpleCalculator();

                this.App = app;
                this.Doc = app.ActiveUIDocument.Document;
                using (transaction = new Transaction(Doc))
                {
                    transaction.Start("auto arrangement");
                    this.symbol = GetFamilySymbol();
                    var ids = app.ActiveUIDocument.Selection.GetElementIds().ToList();
                    if (ids == null || ids.Count == 0)
                    {
                        TaskDialog.Show("Ошибка", "Помещения не выбраны");
                        return;
                    }
                    if (symbol == null)
                    {
                        var dr = TaskDialog.Show("Ошибка загрузки", "Семейство не найдено. Выберите путь...", TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel, TaskDialogResult.Ok);
                        if (dr == TaskDialogResult.Ok)
                        {
                            FileOpenDialog fileOpenDialog = new FileOpenDialog("revit files (*.rfa)|*.rfa|All files (*.*)|*.*");
                            var            fodr           = fileOpenDialog.Show();
                            if (fodr == ItemSelectionDialogResult.Confirmed)
                            {
                                var model  = fileOpenDialog.GetSelectedModelPath();
                                var path   = ModelPathUtils.ConvertModelPathToUserVisiblePath(model);
                                var loaded = Doc.LoadFamilySymbol(path, this.FamilySymbolName, out FamilySymbol loadedFS);
                                if (!loaded)
                                {
                                    TaskDialog.Show("Ошибка загрузки семейства", "Не найдено семейство с названием \"ИП 212-64 прот. R3 ПАСН.425232.038\"");
                                    return;
                                }
                                else
                                {
                                    this.symbol = loadedFS;
                                }
                            }
                        }
                    }
                    if (!this.symbol.IsActive)
                    {
                        this.symbol.Activate();
                    }
                    ids.ForEach(CreateDevices);
                    transaction.Commit();
                }
            }
            catch (Exception ex)
            {
                var s = ex.Message;
                if (transaction != null && transaction.GetStatus() != TransactionStatus.Uninitialized)
                {
                    transaction.RollBack();
                }
            }
        }
Exemplo n.º 3
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document   doc   = uidoc.Document;

            //Create folder dialog path
            FileOpenDialog file_dia = new FileOpenDialog("SAT file (*.sat)|*.sat");

            file_dia.Title = "Select SAT file to import";
            file_dia.Show();
            ModelPath path = file_dia.GetSelectedModelPath();

            file_dia.Dispose();

            //Convert file path to a string
            string path_str = ModelPathUtils.ConvertModelPathToUserVisiblePath(path);

            SATImportOptions satOpt = new SATImportOptions();

            FilteredElementCollector filEle = new FilteredElementCollector(doc);

            IList <Element> views = filEle.OfClass(typeof(View)).ToElements();

            View import_view = views[0] as View;

            try
            {
                using (Transaction trans = new Transaction(doc, "Import SAT"))
                {   // Start transaction, import SAT file and get the element
                    trans.Start();
                    ElementId importedElementId = doc.Import(path_str, satOpt, import_view);
                    Element   importedElement   = doc.GetElement(importedElementId);

                    //Extract geometry element from the imported element
                    Options         geoOptions       = new Options();
                    GeometryElement importedGeometry = importedElement.get_Geometry(geoOptions);


                    //Iterate through the geometry elements extracting the geometry as individual elements
                    foreach (GeometryObject geoObj in importedGeometry)
                    {
                        GeometryInstance instance = geoObj as GeometryInstance;
                        foreach (GeometryObject instObj in instance.SymbolGeometry)
                        {
                            Solid solid = instObj as Solid;
                            FreeFormElement.Create(doc, solid);
                        }
                    }

                    //Delete SAT file

                    doc.Delete(importedElementId);
                    trans.Commit();
                }

                return(Result.Succeeded);
            }

            catch
            {
                TaskDialog.Show("Error Importing", "Something went wrong");
                return(Result.Failed);
            }
        }
Exemplo n.º 4
0
        public async Task <string[]> HandleAsync(string request)
        {
            string[] result;

            Task <string[]> task;

            if (request == "FILEDIALOG")
            {
                task = _revitTask
                       .Run((app) =>
                {
                    //// var document = app.Document;

                    var dialog = new FileOpenDialog("Revit Files (*.rvt)|*.rvt");

                    var dialogResult = dialog.Show();

                    var modelPath = dialog.GetSelectedModelPath();

                    var path = ModelPathUtils
                               .ConvertModelPathToUserVisiblePath(modelPath);

                    return(new[] { path });
                });
            }
            else if (request == "VIEWLIST")
            {
                task = _revitTask
                       .Run((uiapp) =>
                {
                    if (uiapp.ActiveUIDocument?.Document == null)
                    {
                        return(new[] { "No opened documents" });
                    }

                    var document = uiapp.ActiveUIDocument.Document;

                    var plans = new FilteredElementCollector(document)
                                .WhereElementIsNotElementType()
                                .OfClass(typeof(View))
                                .Select(x => x.Name)
                                .ToArray();

                    return(plans);
                });
            }
            else
            {
                task = _revitTask
                       .Run(uiapp =>
                {
                    //// TaskDialog.Show("Deb", $"Requested: {request}");

                    var command = (PostableCommand)Enum.Parse(
                        typeof(PostableCommand),
                        request,
                        true);

                    var id = RevitCommandId
                             .LookupPostableCommandId(command);

                    uiapp.PostCommand(id);

                    return(new[] { $"Successfully posted command {command}" });
                });
            }


            try
            {
                result = await task;
            }
            catch (Exception e)
            {
                result = new[] { $"{e.Message} in {e.StackTrace}" };
            }

            return(result);
        }
Exemplo n.º 5
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            // Determine source document and its unit settings

            TaskDialogResult result = TaskDialog.Show(_taskTitle,
                                                      "Select source file for units copy...",
                                                      TaskDialogCommonButtons.Ok
                                                      | TaskDialogCommonButtons.Cancel);

            if (result == TaskDialogResult.Cancel)
            {
                return(Result.Cancelled);
            }

            FileOpenDialog fileOpen = new FileOpenDialog(
                _fileFilter);

            if (fileOpen.Show() == ItemSelectionDialogResult.Canceled)
            {
                return(Result.Cancelled);
            }

            ModelPath modelPath = fileOpen.GetSelectedModelPath();

            if (modelPath == null)
            {
                return(Result.Failed);
            }

            Document originalDoc = commandData.Application
                                   .OpenAndActivateDocument(modelPath,
                                                            new OpenOptions(), false)?.Document;

            if (originalDoc == null)
            {
                return(Result.Failed);
            }

            DisplayUnit originalDisplayUnits = originalDoc.DisplayUnitSystem;
            Units       originalUnits        = originalDoc.GetUnits();

            if (originalUnits == null)
            {
                return(Result.Failed);
            }

            // Target

            result = TaskDialog.Show(_taskTitle
                                     , string.Format(
                                         "You have selected a document in {0} format.\n"
                                         + "Select target folder to copy units to - "
                                         + "all files will be upgraded & overwritten!",
                                         originalDisplayUnits.ToString())
                                     , TaskDialogCommonButtons.Ok
                                     | TaskDialogCommonButtons.Cancel);

            if (result == TaskDialogResult.Cancel)
            {
                return(Result.Cancelled);
            }

            FolderBrowserDialog folderBrowserDialog
                = new FolderBrowserDialog()
                {
                ShowNewFolderButton = false,
                };
            DialogResult dialogResult = folderBrowserDialog.ShowDialog();

            if (dialogResult == DialogResult.Cancel)
            {
                return(Result.Cancelled);
            }

            DirectoryInfo directoryInfo = new DirectoryInfo(
                folderBrowserDialog.SelectedPath);

            if (!directoryInfo.Exists)
            {
                return(Result.Failed);
            }

            IEnumerable <FileInfo> files = directoryInfo
                                           .EnumerateFiles("*", SearchOption.AllDirectories);

            foreach (FileInfo file in files)
            {
                Document doc = commandData.Application
                               .OpenAndActivateDocument(file.FullName)
                               ?.Document;

                if (doc == null)
                {
                    return(Result.Failed);
                }

                if (originalDoc != null)
                {
                    originalDoc.Close();
                    originalDoc = null;
                }

                using (Transaction t = new Transaction(doc))
                {
                    t.Start("Copy units to target");
                    doc.SetUnits(originalUnits);
                    t.Commit();

                    doc.Save();
                    originalDoc = doc;
                }
            }
            return(Result.Succeeded);
        }