コード例 #1
0
        public static List <Revit.Elements.Element> MassingsByRooms(List <Revit.Elements.Element> Rooms)
        {
            var doc        = DocumentManager.Instance.CurrentDBDocument;
            var MassBICObj = (BuiltInParameter)System.Enum.ToObject(typeof(BuiltInCategory), BuiltInCategory.OST_Mass);
            var objId      = new ElementId(MassBICObj);

            TransactionManager.Instance.EnsureInTransaction(doc);
            var Massings = new List <Revit.Elements.Element>();

            foreach (var room in Rooms)
            {
                var name   = room.InternalElement.LookupParameter("Name").AsString();
                var number = room.InternalElement.LookupParameter("Number").AsString();

                var elem_name  = number + "_" + name;
                var geoobj     = room.InternalElement.get_Geometry(new Options());
                var get_enum   = geoobj.GetEnumerator();
                var next       = get_enum.MoveNext();
                var shape      = get_enum.Current;
                var shape_list = new List <GeometryObject>();
                shape_list.Add(shape);
                var shapeType = DirectShapeType.Create(doc, elem_name, objId);
                shapeType.SetShape(shape_list);
                var lib = DirectShapeLibrary.GetDirectShapeLibrary(doc);
                lib.AddDefinitionType(elem_name, shapeType.Id);
                var element = Autodesk.Revit.DB.DirectShape.CreateElementInstance(doc, shapeType.Id, objId, elem_name, Transform.Identity);
                element.SetTypeId(shapeType.Id);

                Massings.Add(element.ToDSType(true));
            }

            TransactionManager.Instance.TransactionTaskDone();
            return(Massings);
        }
コード例 #2
0
        static IList <GeometryObject> ImportObject(File3dm model, GeometryBase geometry, ObjectAttributes attributes, Document doc, Dictionary <string, Autodesk.Revit.DB.Material> materials, double scaleFactor)
        {
            var layer = model.AllLayers.FindIndex(attributes.LayerIndex);

            if (layer?.IsVisible ?? false)
            {
                using (var ctx = Convert.Context.Push())
                {
                    switch (attributes.MaterialSource)
                    {
                    case ObjectMaterialSource.MaterialFromObject:
                    {
                        var modelMaterial = attributes.MaterialIndex < 0 ? Rhino.DocObjects.Material.DefaultMaterial : model.AllMaterials.FindIndex(attributes.MaterialIndex);
                        ctx.MaterialId = ToHost(modelMaterial, doc, materials);
                        break;
                    }

                    case ObjectMaterialSource.MaterialFromLayer:
                    {
                        var modelLayer    = model.AllLayers.FindIndex(attributes.LayerIndex);
                        var modelMaterial = modelLayer.RenderMaterialIndex < 0 ? Rhino.DocObjects.Material.DefaultMaterial : model.AllMaterials.FindIndex(modelLayer.RenderMaterialIndex);
                        ctx.MaterialId = ToHost(modelMaterial, doc, materials);
                        break;
                    }
                    }

                    if (geometry is InstanceReferenceGeometry instance)
                    {
                        if (model.AllInstanceDefinitions.FindId(instance.ParentIdefId) is InstanceDefinitionGeometry definition)
                        {
                            var definitionId = definition.Id.ToString();
                            var library      = DirectShapeLibrary.GetDirectShapeLibrary(doc);
                            if (!library.Contains(definitionId))
                            {
                                var objectIds = definition.GetObjectIds();
                                var GNodes    = objectIds.
                                                Select(x => model.Objects.FindId(x)).
                                                Cast <File3dmObject>().
                                                SelectMany(x => ImportObject(model, x.Geometry, x.Attributes, doc, materials, scaleFactor));

                                library.AddDefinition(definitionId, GNodes.ToArray());
                            }

                            var xform = instance.Xform.ChangeUnits(scaleFactor);
                            return(DirectShape.CreateGeometryInstance(doc, definitionId, xform.ToHost()));
                        }
                    }
                    else
                    {
                        return(geometry.ToHostMultiple(scaleFactor).ToList());
                    }
                }
            }

            return(new GeometryObject[0]);
        }
コード例 #3
0
        private bool Process(string ifcFilePath, IFCImportOptions options, Document doc)
        {
            TheFileName    = ifcFilePath;
            TheBrepCounter = 0;

            try
            {
                IFCSchemaVersion schemaVersion;
                m_IfcFile     = CreateIFCFile(ifcFilePath, out schemaVersion);
                SchemaVersion = schemaVersion;
            }
            catch (Exception ex)
            {
                Importer.TheLog.LogError(-1, "There was an error reading the IFC file: " + ex.Message + ".  Aborting import.", false);
                return(false);
            }

            Options = options;

            // The DirectShapeLibrary must be reset to potentially remove stale pointers from the last use.
            Document     = doc;
            ShapeLibrary = DirectShapeLibrary.GetDirectShapeLibrary(doc);
            ShapeLibrary.Reset();

            bool readFile = ProcessFile(ifcFilePath);

            if (!readFile)
            {
                return(false);
            }

            m_Transaction = new Transaction(doc);
            bool success = true;

            switch (options.Intent)
            {
            case IFCImportIntent.Reference:
                success = ProcessReference();
                break;
            }
            if (success)
            {
                StoreIFCCreatorInfo(m_IfcFile, doc.ProjectInformation);
            }

            return(success);
        }
コード例 #4
0
        private static void CreateDirectShape(Document doc, IList <GeometryObject> geometry)
        {
            DirectShapeLibrary directShapeLibrary = DirectShapeLibrary.GetDirectShapeLibrary(doc);
            DirectShapeType    directShapeType    = DirectShapeType.Create(doc, "NavisWorksShape", new ElementId(BuiltInCategory.OST_GenericModel));

            directShapeType.SetShape(geometry);
            directShapeLibrary.AddDefinitionType("NavisWorksShape", directShapeType.Id);

            DirectShape ds = DirectShape.CreateElementInstance(doc, directShapeType.Id, directShapeType.Category.Id, "NavisWorksShape", Transform.Identity);

            ds.SetTypeId(directShapeType.Id);
            ds.ApplicationId     = Assembly.GetExecutingAssembly().GetType().GUID.ToString();
            ds.ApplicationDataId = Guid.NewGuid().ToString();

            DirectShapeOptions dsOptions = ds.GetOptions();

            dsOptions.ReferencingOption = DirectShapeReferencingOption.Referenceable;
            ds.SetOptions(dsOptions);

            ds.SetShape(geometry);
        }
コード例 #5
0
        public static Result Import3DMFile(string filePath, Document doc, BuiltInCategory builtInCategory)
        {
            try
            {
                DirectShapeLibrary.GetDirectShapeLibrary(doc).Reset();

                using (var model = File3dm.Read(filePath))
                {
                    var scaleFactor = RhinoMath.UnitScale(model.Settings.ModelUnitSystem, Revit.ModelUnitSystem);

                    using (var trans = new Transaction(doc, "Import 3D Model"))
                    {
                        if (trans.Start() == TransactionStatus.Started)
                        {
                            var categoryId = new ElementId(builtInCategory);
                            var materials  = GetMaterialsByName(doc);

                            var type = DirectShapeType.Create(doc, Path.GetFileName(filePath), categoryId);

                            foreach (var obj in model.Objects.Where(x => !x.Attributes.IsInstanceDefinitionObject && x.Attributes.Space == ActiveSpace.ModelSpace))
                            {
                                if (!obj.Attributes.Visible)
                                {
                                    continue;
                                }

                                var geometryList = ImportObject(model, obj.Geometry, obj.Attributes, doc, materials, scaleFactor).ToArray();
                                if (geometryList == null)
                                {
                                    continue;
                                }

                                try { type.AppendShape(geometryList); }
                                catch (Autodesk.Revit.Exceptions.ArgumentException) { }
                            }

                            var ds = DirectShape.CreateElement(doc, type.Category.Id);
                            ds.SetTypeId(type.Id);

                            var library = DirectShapeLibrary.GetDirectShapeLibrary(doc);
                            if (!library.ContainsType(type.UniqueId))
                            {
                                library.AddDefinitionType(type.UniqueId, type.Id);
                            }

                            ds.SetShape(DirectShape.CreateGeometryInstance(doc, type.UniqueId, Autodesk.Revit.DB.Transform.Identity));

                            if (trans.Commit() == TransactionStatus.Committed)
                            {
                                var elements = new ElementId[] { ds.Id };
                                Revit.ActiveUIDocument.Selection.SetElementIds(elements);
                                Revit.ActiveUIDocument.ShowElements(elements);

                                return(Result.Succeeded);
                            }
                        }
                    }
                }
            }
            finally
            {
                DirectShapeLibrary.GetDirectShapeLibrary(doc).Reset();
            }

            return(Result.Failed);
        }
コード例 #6
0
ファイル: Sample8.cs プロジェクト: zjloscar/rhino.inside
        static IList <GeometryObject> ImportObject(File3dm model, GeometryBase geometry, ObjectAttributes attributes, Document doc, Dictionary <string, Autodesk.Revit.DB.Material> materials, double scaleFactor)
        {
            using (var ga = Convert.GraphicAttributes.Push())
            {
                switch (attributes.MaterialSource)
                {
                case ObjectMaterialSource.MaterialFromObject:
                {
                    var modelMaterial = attributes.MaterialIndex < 0 ? Rhino.DocObjects.Material.DefaultMaterial : model.AllMaterials.FindIndex(attributes.MaterialIndex);
                    ga.MaterialId = ToHost(modelMaterial, doc, materials);
                    break;
                }

                case ObjectMaterialSource.MaterialFromLayer:
                {
                    var modelLayer    = model.AllLayers.FindIndex(attributes.LayerIndex);
                    var modelMaterial = modelLayer.RenderMaterialIndex < 0 ? Rhino.DocObjects.Material.DefaultMaterial : model.AllMaterials.FindIndex(modelLayer.RenderMaterialIndex);
                    ga.MaterialId = ToHost(modelMaterial, doc, materials);
                    break;
                }
                }

                if (geometry is InstanceReferenceGeometry instance)
                {
                    if (model.AllInstanceDefinitions.FindId(instance.ParentIdefId) is InstanceDefinitionGeometry definition)
                    {
                        var objectIds = definition.GetObjectIds();

                        // Compute a definition ID that includes InstanceDefinition Name, Id and content object Ids
                        var definitionId = definition.Name;
                        {
                            var data = new byte[(objectIds.Length + 1) * 16];

                            Buffer.BlockCopy(instance.ParentIdefId.ToByteArray(), 0, data, 0, 16);
                            for (int i = 0; i < objectIds.Length; i++)
                            {
                                Buffer.BlockCopy(objectIds[i].ToByteArray(), 0, data, (i * 16), 16);
                            }

                            using (var sha256 = System.Security.Cryptography.SHA256.Create())
                                definitionId += $"[{ByteArrayToString(sha256.ComputeHash(data))}]";
                        }

                        var library = DirectShapeLibrary.GetDirectShapeLibrary(doc);
                        if (!library.Contains(definitionId))
                        {
                            var GNodes = objectIds.
                                         Select(x => model.Objects.FindId(x)).
                                         Cast <File3dmObject>().
                                         SelectMany(x => ImportObject(model, x.Geometry, x.Attributes, doc, materials, scaleFactor));

                            library.AddDefinition(definitionId, GNodes.ToArray());
                        }

                        var xform = instance.Xform;

                        xform.Affineize();
                        xform.DecomposeAffine(out Vector3d translation, out var linear);
                        xform = Rhino.Geometry.Transform.Translation(translation * scaleFactor) * linear;

                        return(DirectShape.CreateGeometryInstance(doc, definitionId, xform.ToHost()));
                    }

                    return(new GeometryObject[0]);
                }
                else
                {
                    return(geometry.ToHost(scaleFactor).ToList());
                }
            }
        }
コード例 #7
0
ファイル: SATtoDirectShape.cs プロジェクト: chuongmep/ReviTab
        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;
            View          activeView = doc.ActiveView;

            List <Element> col = new List <Element>();

            Reference r = uidoc.Selection.PickObject(ObjectType.Element, "Select Element");

            col.Add(doc.GetElement(r));

            Options geometryOptions = new Options();

            ElementId cat1Id = new ElementId(BuiltInCategory.OST_Walls);


            DirectShapeLibrary dsLib = DirectShapeLibrary.GetDirectShapeLibrary(doc);

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Convert elements to DirectShapes");

                foreach (Element e in col)
                {
                    GeometryElement gelt = e.get_Geometry(
                        geometryOptions);

                    if (null != gelt)
                    {
                        string appDataGUID = e.Id.ToString();

                        try
                        {
                            string          familyName = "MyFamily";
                            DirectShapeType dsType1    = DirectShapeType.Create(doc, familyName, cat1Id);
                            dsType1.SetShape(new List <GeometryObject>(gelt));
                            dsLib.AddDefinitionType(familyName, dsType1.Id);

                            Transform trs = Transform.Identity;

                            DirectShape ds1 = DirectShape.CreateElementInstance(doc, dsType1.Id, cat1Id, familyName, trs);

                            doc.Delete(e.Id);

                            TaskDialog.Show("Result", "Element Flattened");
                        }
                        catch (Exception ex)
                        {
                            TaskDialog.Show("Error", ex.Message);
                        }
                    }
                }
                tx.Commit();
            }

            return(Result.Succeeded);
        }
コード例 #8
0
        private bool Process(string ifcFilePath, IFCImportOptions options, Document doc)
        {
            // Manually set to false if necessary for debugging.
            CleanEntitiesAfterCreate = true;

            TheFileName    = ifcFilePath;
            TheBrepCounter = 0;

            Log = IFCImportLog.CreateLog(ifcFilePath + ".log.html");

            try
            {
                IFCSchemaVersion schemaVersion;
                m_IfcFile     = CreateIFCFile(ifcFilePath, out schemaVersion);
                SchemaVersion = schemaVersion;
            }
            catch (Exception ex)
            {
                Log.LogError(-1, "There was an error reading the IFC file: " + ex.Message + ".  Aborting import.", false);
                return(false);
            }

            Options = options;

            Document     = doc;
            ShapeLibrary = DirectShapeLibrary.GetDirectShapeLibrary(doc);
            ShapeLibrary.Reset();

            IFCFileReadOptions readOptions = new IFCFileReadOptions();

            readOptions.FileName          = ifcFilePath;
            readOptions.XMLConfigFileName = Path.Combine(RevitProgramPath, "EDM\\ifcXMLconfiguration.xml");

            int numErrors   = 0;
            int numWarnings = 0;

            try
            {
                Importer.TheCache.StatusBar.Set(String.Format(Resources.IFCReadingFile, TheFileName));
                m_IfcFile.Read(readOptions, out numErrors, out numWarnings);
            }
            catch (Exception ex)
            {
                Log.LogError(-1, "There was an error reading the IFC file: " + ex.Message + ".  Aborting import.", false);
                return(false);
            }

            if (numErrors > 0 || numWarnings > 0)
            {
                if (numErrors > 0)
                {
                    if (numWarnings > 0)
                    {
                        Log.LogError(-1, "There were " + numErrors + " errors and " + numWarnings + " reading the IFC file.  Please look at the log information at the end of this report for more information.", false);
                    }
                    else
                    {
                        Log.LogError(-1, "There were " + numErrors + " errors reading the IFC file.  Please look at the log information at the end of this report for more information.", false);
                    }
                }
                else
                {
                    Log.LogWarning(-1, "There were " + numWarnings + " warnings reading the IFC file.  Please look at the log information at the end of this report for more information.", false);
                }
            }

            m_Transaction = new Transaction(doc);
            switch (options.Intent)
            {
            case IFCImportIntent.Reference:
                InitializeOpenTransaction("Open IFC Reference File");

                //If there is more than one project, we will be ignoring all but the first one.
                IList <IFCAnyHandle> projects = IFCImportFile.TheFile.GetInstances(IFCEntityType.IfcProject, false);
                if (projects.Count == 0)
                {
                    Log.LogError(-1, "There were no IfcProjects found in the file.  Aborting import.", false);
                    return(false);
                }
                else
                {
                    IFCProject.ProcessIFCProject(projects[0]);
                }
                break;
            }

            return(true);
        }