Exemplo n.º 1
0
 static IList <GeometryObject> ImportObject(GeometryBase geometry, ObjectAttributes attributes, double scaleFactor)
 {
     return(geometry.ToHost(scaleFactor).ToList());
 }
Exemplo n.º 2
0
        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());
                }
            }
        }