Beispiel #1
0
        /// <summary>
        /// Export an entire Rhino instance definition by turning it into a Mitsuba shapegroup
        /// </summary>
        /// <param name="parent">The parent node in the output XML document</param>
        /// <param name="idef">The InstanceDefinition instance to be exported</param>
        /// <returns>true if any content was exported</returns>
        private void ExportInstanceDef(XmlElement parent, InstanceDefinition idef)
        {
            if (!idef.InUse(1))
            {
                return;
            }

            var id = GetID("group");

            _idMap.Add(idef.Id, id);
            var shapeElement = _xmlDocument.CreateElement("shape");

            if (idef.Name.Length > 0)
            {
                shapeElement.AppendChild(_xmlDocument.CreateComment(" Rhino object '" + idef.Name + "' "));
            }
            shapeElement.SetAttribute("type", "shapegroup");
            shapeElement.SetAttribute("id", id);

            var objects = idef.GetObjects();
            var success = false;

            foreach (var o in objects)
            {
                success |= ExportRenderMesh(shapeElement, o);
            }

            if (success)
            {
                parent.AppendChild(shapeElement);
            }
        }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List <string> bID = new List <string>();

            if (!DA.GetDataList(0, bID))
            {
                return;
            }

            DataTree <GeometryBase> gbs = new DataTree <GeometryBase>();

            for (int i = 0; i < bID.Count; i++)
            {
                InstanceDefinition def = RhinoDoc.ActiveDoc.InstanceDefinitions.Find(bID[i]);
                if (def == null)
                {
                    gbs.Add(null, new GH_Path(i));
                    continue;
                }
                RhinoObject[] objects = def.GetObjects();
                GeometryBase  gb;
                for (int j = 0; j < objects.Length; j++)
                {
                    gb = objects[j].DuplicateGeometry();
                    gbs.Add(gb, new GH_Path(i));
                }
            }

            DA.SetDataTree(0, gbs);
        }
Beispiel #3
0
        private static bool _changeInstanceDefinitionGeometryLayer(InstanceDefinition definition, RhinoDoc doc, Layer layer)
        {
            if (!doc.Layers.Contains(layer))
            {
                RhinoApp.WriteLine($"No Layer {layer} in document!");
                return(false);
            }

            var rhObjects  = definition.GetObjects();
            var geometries = new GeometryBase[rhObjects.Length];
            var attributes = new ObjectAttributes[rhObjects.Length];

            for (int i = 0; i < rhObjects.Length; i++)
            {
                geometries[i]            = rhObjects[i].Geometry;
                attributes[i]            = rhObjects[i].Attributes;
                attributes[i].LayerIndex = layer.Index;
            }

            var serial  = doc.BeginUndoRecord("Change Block geometry layer");
            var success = doc.InstanceDefinitions.ModifyGeometry(definition.Index, geometries, attributes);

            doc.EndUndoRecord(serial);
            return(success);
        }
Beispiel #4
0
 public static IEnumerable <ChildBlockInsertionParameters> GetPartRelativeXforms(this InstanceDefinition definition)
 {
     return(from obj in definition.GetObjects()
            where obj.ObjectType == ObjectType.InstanceReference
            let reference = obj as InstanceObject
                            select new ChildBlockInsertionParameters(reference.InsertionPoint, reference.InstanceXform));
 }
Beispiel #5
0
        private RhinoSceneHierarchyNode GetOrCreateDefinitionRootNode(InstanceDefinition InInstanceDefinition)
        {
            RhinoSceneHierarchyNode InstanceRootNode;

            //If a hierarchy node does not exist for this instance definition, create one.
            if (!InstanceDefinitionHierarchyNodeDictionary.TryGetValue(InInstanceDefinition, out InstanceRootNode))
            {
                const bool bIsInstanceDefinition = true;
                const int  MaterialIndex         = -1;

                RhinoSceneHierarchyNodeInfo DefinitionNodeInfo = GenerateNodeInfo(InInstanceDefinition, bIsInstanceDefinition, MaterialIndex, Transform.Identity);
                InstanceRootNode = new RhinoSceneHierarchyNode(/*bInIsInstanceDefinition=*/ true, DefinitionNodeInfo);
                InstanceDefinitionHierarchyNodeDictionary.Add(InInstanceDefinition, InstanceRootNode);

                RhinoObject[] InstanceObjects = InInstanceDefinition.GetObjects();
                RecursivelyParseObjectInstance(InstanceObjects, InstanceRootNode);
            }

            return(InstanceRootNode);
        }
        /// <summary>
        /// Export an entire Rhino instance definition by turning it into a Mitsuba shapegroup
        /// </summary>
        /// <param name="parent">The parent node in the output XML document</param>
        /// <param name="obj">The InstanceDefinition instance to be exported</param>
        /// <returns>true if any content was exported</returns>
        private bool ExportInstanceDef(XmlElement parent, InstanceDefinition idef)
        {
            if (!idef.InUse(1))
            {
                return(false);
            }

            string id = GetID("group");

            m_idMap.Add(idef.Id, id);

            XmlElement shapeElement = m_xmlDocument.CreateElement("shape");

            if (idef.Name.Length > 0)
            {
                shapeElement.AppendChild(m_xmlDocument.CreateComment(" Rhino object '" + idef.Name + "' "));
            }

            shapeElement.SetAttribute("type", "shapegroup");
            shapeElement.SetAttribute("id", id);

            RhinoObject[] objects = idef.GetObjects();

            bool success = false;

            foreach (RhinoObject o in objects)
            {
                success |= ExportRenderMesh(shapeElement, o, false);
            }

            if (success)
            {
                parent.AppendChild(shapeElement);
            }
            return(success);
        }
Beispiel #7
0
        public Tile(InstanceDefinition definition, double density, RhinoDoc doc)
        {
            Index = definition.Index;

            var geometry = definition.GetObjects();

            int renderIndex = doc.Layers.FindName("Render").Index;

            var renderMeshes = geometry
                               .Where(g => g.Attributes.LayerIndex == renderIndex)
                               .Select(g => g.Geometry as Mesh)
                               .ToList();

            Renderers = renderMeshes.Select(m => new MeshExport(m)).ToList();

            int collisionsIndex = doc.Layers.FindName("Collision").Index;

            var meshColliders = geometry
                                .Where(g => g.Attributes.LayerIndex == collisionsIndex)
                                .Select(g => g.Geometry as Mesh)
                                .ToList();

            Colliders = meshColliders
                        .Select(m => m.Offset(0.001))
                        .Select(m => new MeshExport(m))
                        .ToList();

            Point3d centroid = Point3d.Origin;
            double  mass     = 0.0;

            foreach (var mesh in meshColliders)
            {
                var    prop        = VolumeMassProperties.Compute(mesh);
                double elementMass = prop.Volume * density;
                centroid += prop.Centroid * elementMass;
                mass     += elementMass;
            }

            centroid /= mass;
            Centroid  = new Vector3Export(centroid);
            Mass      = (float)mass;

            int facesIndex = doc.Layers.FindName("Faces").Index;

            //Faces = geometry
            //         .Where(g => g.Attributes.LayerIndex == facesIndex)
            //         .Select(g =>
            //          {
            //              Curve curve = g.Geometry as Curve;
            //              curve.TryGetPolyline(out Polyline pl);
            //              var plane = new Plane(pl[1], pl[2], pl[0]);
            //              plane.Origin = (pl[0] * 0.5 + pl[2] * 0.5);
            //              return new Orient(plane);
            //          }).ToList();

            Faces = geometry
                    .Where(g => g.Attributes.LayerIndex == facesIndex)
                    .Select(g =>
            {
                var point = g.Geometry as Point;
                return(new Vector3Export(point.Location));
            }).ToList();
        }
Beispiel #8
0
 /// <summary>
 /// Gets all <see cref="InstanceObject"/>s assembled in the given <see cref="InstanceDefinition"/>
 /// </summary>
 /// <param name="definition"></param>
 /// <returns></returns>
 public static IEnumerable <InstanceObject> GetPartInstances(this InstanceDefinition definition)
 {
     return(from obj in definition.GetObjects()
            where obj.ObjectType == ObjectType.InstanceReference
            select(InstanceObject) obj);
 }
Beispiel #9
0
 /// <summary>
 /// Tests a given instance definition, if it is at the root level
 /// meaning it only contains "pure" rhino geometry and no other nested instance definitions.
 /// </summary>
 /// <param name="definition"></param>
 /// <returns>A boolean telling if the given definition is root or not</returns>
 public static bool IsRoot(this InstanceDefinition definition)
 {
     return(definition.GetObjects().All(o => o.ObjectType != ObjectType.InstanceReference));
 }