Exemplo n.º 1
0
        void CommitInstance
        (
            Document doc, IGH_DataAccess DA, int Iteration,
            Rhino.Geometry.Mesh mesh
        )
        {
            var element = PreviousElement(doc, Iteration);

            try
            {
                if (mesh == null)
                {
                    throw new NullReferenceException($"Parameter '{Params.Input[0].Name}' not set to an instance of a Mesh.");
                }

                if (!mesh.IsValidWithLog(out var log))
                {
                    foreach (var line in log.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, line);
                    }

                    throw new Exception($"Parameter '{Params.Input[0].Name}' not set to a valid Mesh.");
                }

                var scaleFactor = 1.0 / Revit.ModelUnits;
                if (scaleFactor != 1.0)
                {
                    mesh.Scale(scaleFactor);
                }

                var shape = new List <GeometryObject>();

                foreach (var geometry in mesh.ToHost().SelectMany(x => x.ToDirectShapeGeometry()))
                {
                    // DirectShape only accepts those types and no nulls
                    switch (geometry)
                    {
                    case Point p: shape.Add(p); break;

                    case Curve c: shape.Add(c); break;

                    case Solid s: shape.Add(s); break;

                    case Mesh m: shape.Add(m); break;
                    }
                }

                var ds = element as DirectShape ?? CopyParametersFrom(DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)), element);
                ds.SetShape(shape);
                element = ds;

                ReplaceElement(doc, DA, Iteration, element);
            }
            catch (Exception e)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message);
                ReplaceElement(doc, DA, Iteration, null);
            }
        }
Exemplo n.º 2
0
        void ReconstructDirectShapeByMesh
        (
            Document doc,
            ref Autodesk.Revit.DB.Element element,

            Rhino.Geometry.Mesh mesh
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

            ThrowIfNotValid(nameof(mesh), mesh);

            if (element is DirectShape ds)
            {
            }
            else
            {
                ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel));
            }

            var shape = mesh.
                        ToHost(scaleFactor).
                        SelectMany(x => x.ToDirectShapeGeometry());

            ds.SetShape(shape.ToList());

            ReplaceElement(ref element, ds);
        }
Exemplo n.º 3
0
        void CommitInstance
        (
            Document doc, IGH_DataAccess DA, int Iteration,
            Rhino.Geometry.Mesh mesh
        )
        {
            var element = PreviousElement(doc, Iteration);

            try
            {
                if (mesh == null || !mesh.IsValid)
                {
                    throw new Exception(string.Format("Parameter '{0}' must be valid Mesh.", Params.Input[0].Name));
                }

                var scaleFactor = 1.0 / Revit.ModelUnits;
                if (scaleFactor != 1.0)
                {
                    mesh.Scale(scaleFactor);
                }

                var shape = new List <GeometryObject>();

                foreach (var geometry in mesh.ToHost().SelectMany(x => x.ToDirectShapeGeometry()))
                {
                    // DirectShape only accepts those types and no nulls
                    switch (geometry)
                    {
                    case Point p: shape.Add(p); break;

                    case Curve c: shape.Add(c); break;

                    case Solid s: shape.Add(s); break;

                    case Mesh m: shape.Add(m); break;
                    }
                }

                var ds = element as DirectShape ?? CopyParametersFrom(DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)), element);
                ds.SetShape(shape);
                element = ds;

                ReplaceElement(doc, DA, Iteration, element);
            }
            catch (Exception e)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message);
                ReplaceElement(doc, DA, Iteration, null);
            }
        }