Exemplo n.º 1
0
        // Main execution
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;

            currentDomain.AssemblyResolve += new ResolveEventHandler(Misc.LoadFromSameFolder);

            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;
            Document      doc   = uidoc.Document;

            View view = doc.ActiveView;

            Selection sel = uidoc.Selection;

            double tolerance = app.ShortCurveTolerance;


            // Check if the families are ready
            if (Properties.Settings.Default.name_door == null ||
                Properties.Settings.Default.name_window == null)
            {
                System.Windows.MessageBox.Show("Please select the door/window type in settings", "Tips");
                return(Result.Cancelled);
            }
            //if (!File.Exists(Properties.Settings.Default.url_door) && File.Exists(Properties.Settings.Default.url_window))
            //{
            //    System.Windows.MessageBox.Show("Please check the family path is solid", "Tips");
            //    return Result.Cancelled;
            //}
            //Family fDoor, fWindow = null;
            //using (Transaction tx = new Transaction(doc, "Load necessary families"))
            //{
            //    tx.Start();
            //    if (!doc.LoadFamily(Properties.Settings.Default.url_door, out fDoor) ||
            //    !doc.LoadFamily(Properties.Settings.Default.url_window, out fWindow))
            //    {
            //        System.Windows.MessageBox.Show("Loading family failed", "Tips");
            //        return Result.Cancelled;
            //    }
            //    tx.Commit();
            //}


            // Pick Import Instance
            ImportInstance import = null;

            try
            {
                Reference r = uidoc.Selection.PickObject(ObjectType.Element, new Util.ElementsOfClassSelectionFilter <ImportInstance>());
                import = doc.GetElement(r) as ImportInstance;
            }
            catch
            {
                return(Result.Cancelled);
            }
            if (import == null)
            {
                System.Windows.MessageBox.Show("CAD not found", "Tips");
                return(Result.Cancelled);
            }


            // Fetch baselines
            List <Curve> doorCrvs     = new List <Curve>();
            List <Curve> windowCrvs   = new List <Curve>();
            List <Curve> wallCrvs     = new List <Curve>();
            var          wallLayers   = Misc.GetLayerNames(Properties.Settings.Default.layerWall);
            var          doorLayers   = Misc.GetLayerNames(Properties.Settings.Default.layerDoor);
            var          windowLayers = Misc.GetLayerNames(Properties.Settings.Default.layerWindow);

            try
            {
                foreach (string doorLayer in doorLayers)
                {
                    doorCrvs.AddRange(Util.TeighaGeometry.ShatterCADGeometry(uidoc, import, doorLayer, tolerance));
                }
                foreach (string windowLayer in windowLayers)
                {
                    windowCrvs.AddRange(Util.TeighaGeometry.ShatterCADGeometry(uidoc, import, windowLayer, tolerance));
                }
                foreach (string wallLayer in wallLayers)
                {
                    wallCrvs.AddRange(Util.TeighaGeometry.ShatterCADGeometry(uidoc, import, wallLayer, tolerance));
                }
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show(e.Message, "Tips");
                return(Result.Cancelled);
            }
            if (doorCrvs == null || windowCrvs == null || wallCrvs == null || doorCrvs.Count * windowCrvs.Count * wallCrvs.Count == 0)
            {
                System.Windows.MessageBox.Show("Baselines not found", "Tips");
                return(Result.Cancelled);
            }


            // Grab the current building level
            FilteredElementCollector docLevels = new FilteredElementCollector(doc)
                                                 .WhereElementIsNotElementType()
                                                 .OfCategory(BuiltInCategory.INVALID)
                                                 .OfClass(typeof(Level));
            ICollection <Element> levels = docLevels.OfClass(typeof(Level)).ToElements();
            Level defaultLevel           = null;

            foreach (Level level in levels)
            {
                if (level.Id == import.LevelId)
                {
                    defaultLevel = level;
                }
            }
            if (defaultLevel == null)
            {
                System.Windows.MessageBox.Show("Please make sure there's a base level in current view", "Tips");
                return(Result.Cancelled);
            }


            // Convert texts info into TextNote by Teigha
            // Revit does not expose any API to parse text info
            string path = Util.TeighaText.GetCADPath(uidoc, import);
            List <Util.TeighaText.CADTextModel> labels = Util.TeighaText.GetCADText(path);

            /*
             * List<UtilGetCADText.CADTextModel> labels = new List<UtilGetCADText.CADTextModel>();
             * foreach (UtilGetCADText.CADTextModel text in UtilGetCADText.GetCADText(path))
             * {
             *  if (UtilGetCADText.IsLabel(text.Text, out string label))
             *  {
             *      labels.Add(text);
             *  }
             * }
             */

            Debug.Print("The path of linked DWG file is: " + path);
            Debug.Print("Lables in total: " + labels.Count.ToString());
            foreach (Util.TeighaText.CADTextModel label in labels)
            {
                Debug.Print(label.Text);
            }


            /*
             * // LineStyle filter
             * CurveElementFilter filter = new CurveElementFilter(CurveElementType.ModelCurve);
             * FilteredElementCollector collector = new FilteredElementCollector(doc);
             * ICollection<Element> founds = collector.WherePasses(filter).ToElements();
             * List<CurveElement> importCurves = new List<CurveElement>();
             *
             * foreach (CurveElement ce in founds)
             * {
             *  importCurves.Add(ce);
             * }
             *
             * var doorCurves = importCurves.Where(x => x.LineStyle.Name == "DOOR").ToList();
             * List<Curve> doorCrvs = new List<Curve>();  // The door block has one arc at least
             * foreach (CurveElement ce in doorCurves)
             * {
             *  doorCrvs.Add(ce.GeometryCurve as Curve);
             * }
             *
             * var wallCurves = importCurves.Where(x => x.LineStyle.Name == "WALL").ToList();
             * List<Line> wallLines = new List<Line>();  // Algorithm only support walls of line type
             * foreach (CurveElement ce in wallCurves)
             * {
             *  wallLines.Add(ce.GeometryCurve as Line);
             * }
             * var windowCurves = importCurves.Where(x => x.LineStyle.Name == "WINDOW").ToList();
             * List<Curve> windowCrvs = new List<Curve>();
             * foreach (CurveElement ce in windowCurves)
             * {
             *  windowCrvs.Add(ce.GeometryCurve as Curve);
             * }
             */

            TransactionGroup tg = new TransactionGroup(doc, "Create openings");

            try
            {
                tg.Start();
                CreateOpening.Execute(doc, doorCrvs, windowCrvs, wallCrvs, labels,
                                      Properties.Settings.Default.name_door,
                                      Properties.Settings.Default.name_window,
                                      defaultLevel, false);
                tg.Assimilate();
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show(e.Message);
                tg.RollBack();
                return(Result.Cancelled);
            }

            return(Result.Succeeded);
        }
Exemplo n.º 2
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;

            double tolerance = app.ShortCurveTolerance;


            // Check if the families are ready
            if (Properties.Settings.Default.name_columnRect == null ||
                Properties.Settings.Default.name_columnRound == null)
            {
                System.Windows.MessageBox.Show("Please select the column type in settings", "Tips");
                return(Result.Cancelled);
            }
            //if (!File.Exists(Properties.Settings.Default.url_column))
            //{
            //    System.Windows.MessageBox.Show("Please check the family path is solid", "Tips");
            //    return Result.Cancelled;
            //}
            //Family fColumn;
            //using (Transaction tx = new Transaction(doc, "Load necessary families"))
            //{
            //    tx.Start();
            //    doc.LoadFamily(Properties.Settings.Default.url_column, new Util.SampleFamilyLoadOptions(), out fColumn);
            //    tx.Commit();
            //}


            // Pick Import Instance
            ImportInstance import = null;

            try
            {
                Reference r = uidoc.Selection.PickObject(ObjectType.Element, new Util.ElementsOfClassSelectionFilter <ImportInstance>());
                import = doc.GetElement(r) as ImportInstance;
            }
            catch
            {
                return(Result.Cancelled);
            }
            if (import == null)
            {
                System.Windows.MessageBox.Show("CAD not found", "Tips");
                return(Result.Cancelled);
            }


            // Fetch baselines
            List <Curve> columnCrvs   = new List <Curve>();
            var          columnLayers = Misc.GetLayerNames(Properties.Settings.Default.layerColumn);

            try
            {
                foreach (string columnLayer in columnLayers)
                {
                    columnCrvs.AddRange(Util.TeighaGeometry.ShatterCADGeometry(uidoc, import, columnLayer, tolerance));
                }
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show(e.Message, "Tips");
                return(Result.Cancelled);
            }
            if (columnCrvs == null || columnCrvs.Count == 0)
            {
                System.Windows.MessageBox.Show("Baseline not found", "Tips");
                return(Result.Cancelled);
            }


            // Grab the current building level
            FilteredElementCollector docLevels = new FilteredElementCollector(doc)
                                                 .WhereElementIsNotElementType()
                                                 .OfCategory(BuiltInCategory.INVALID)
                                                 .OfClass(typeof(Level));
            ICollection <Element> levels = docLevels.OfClass(typeof(Level)).ToElements();
            Level defaultLevel           = null;

            foreach (Level level in levels)
            {
                if (level.Id == import.LevelId)
                {
                    defaultLevel = level;
                }
            }
            if (defaultLevel == null)
            {
                System.Windows.MessageBox.Show("Please make sure there's a base level in current view", "Tips");
                return(Result.Cancelled);
            }


            // Start batching
            TransactionGroup tg = new TransactionGroup(doc, "Create columns");

            try
            {
                tg.Start();
                CreateColumn.Execute(app, doc, columnCrvs,
                                     Properties.Settings.Default.name_columnRect,
                                     Properties.Settings.Default.name_columnRound,
                                     defaultLevel, false);
                tg.Assimilate();
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show(e.Message);
                tg.RollBack();
                return(Result.Cancelled);
            }

            return(Result.Succeeded);
        }
Exemplo n.º 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;

            double tolerance = app.ShortCurveTolerance;


            // Pick Import Instance
            ImportInstance import = null;

            try
            {
                Reference r = uidoc.Selection.PickObject(ObjectType.Element, new Util.ElementsOfClassSelectionFilter <ImportInstance>());
                import = doc.GetElement(r) as ImportInstance;
            }
            catch
            {
                return(Result.Cancelled);
            }
            if (import == null)
            {
                System.Windows.MessageBox.Show("CAD not found", "Tips");
                return(Result.Cancelled);
            }


            // Fetch baselines
            List <Curve> wallCrvs   = new List <Curve>();
            var          wallLayers = Misc.GetLayerNames(Properties.Settings.Default.layerWall);

            try
            {
                foreach (string wallLayer in wallLayers)
                {
                    wallCrvs.AddRange(Util.TeighaGeometry.ShatterCADGeometry(uidoc, import, wallLayer, tolerance));
                }
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show(e.Message, "Tips");
                return(Result.Cancelled);
            }
            if (wallCrvs == null || wallCrvs.Count == 0)
            {
                System.Windows.MessageBox.Show("Baselines not found", "Tips");
                return(Result.Cancelled);
            }


            // Grab the current building level
            FilteredElementCollector docLevels = new FilteredElementCollector(doc)
                                                 .WhereElementIsNotElementType()
                                                 .OfCategory(BuiltInCategory.INVALID)
                                                 .OfClass(typeof(Level));
            ICollection <Element> levels = docLevels.OfClass(typeof(Level)).ToElements();
            Level defaultLevel           = null;

            foreach (Level level in levels)
            {
                if (level.Id == import.LevelId)
                {
                    defaultLevel = level;
                }
            }
            if (defaultLevel == null)
            {
                System.Windows.MessageBox.Show("Please make sure there's a base level in current view", "Tips");
                return(Result.Cancelled);
            }


            TransactionGroup tg = new TransactionGroup(doc, "Create walls");

            try
            {
                tg.Start();
                CreateWall.Execute(uiapp, wallCrvs, defaultLevel, false);
                tg.Assimilate();
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show(e.Message);
                tg.RollBack();
                return(Result.Cancelled);
            }

            return(Result.Succeeded);
        }