static DB.GeometryObject ToShape(Brep brep, double factor)
        {
            // Try using DB.BRepBuilder
            if (BrepEncoder.ToSolid(brep, factor) is DB.Solid solid)
            {
                return(solid);
            }

            Debug.WriteLine("Try meshing the brep.");
            return(BrepEncoder.ToMesh(brep, factor));
        }
Beispiel #2
0
        public static DB.GeometryObject ToGeometryObject(this GeometryBase geometry, double scaleFactor)
        {
            switch (geometry)
            {
            case Point point: return(point.ToPoint(scaleFactor));

            case Curve curve: return(curve.ToCurve(scaleFactor));

            case Brep brep: return(brep.ToSolid(scaleFactor));

            case Mesh mesh: return(mesh.ToMesh(scaleFactor));

            case Extrusion extrusion:
            {
                var brep = extrusion.ToBrep();
                if (BrepEncoder.EncodeRaw(ref brep, scaleFactor))
                {
                    return(BrepEncoder.ToSolid(brep));
                }
            }
            break;

            case SubD subD:
            {
                var brep = subD.ToBrep();
                if (BrepEncoder.EncodeRaw(ref brep, scaleFactor))
                {
                    return(BrepEncoder.ToSolid(brep));
                }
            }
            break;

            default:
                if (geometry.HasBrepForm)
                {
                    var brepForm = Brep.TryConvertBrep(geometry);
                    if (BrepEncoder.EncodeRaw(ref brepForm, scaleFactor))
                    {
                        return(BrepEncoder.ToSolid(brepForm));
                    }
                }
                break;
            }

            throw new ConversionException($"Unable to convert {geometry} to Autodesk.Revit.DB.GeometryObject");
        }
        internal static IEnumerable <DB.GeometryObject> ToGeometryObjectMany(Brep brep)
        {
            var solid = BrepEncoder.ToSolid(brep);

            if (solid is object)
            {
                yield return(solid);

                yield break;
            }

            if (brep.Faces.Count > 1)
            {
                Debug.WriteLine("Try exploding the brep and converting face by face.");

                var breps = brep.UnjoinEdges(brep.Edges.Select(x => x.EdgeIndex));
                foreach (var face in breps.SelectMany(x => ToGeometryObjectMany(x)))
                {
                    yield return(face);
                }
            }
            else
            {
                Debug.WriteLine("Try meshing the brep.");

                // Emergency result as a mesh
                var mp = MeshingParameters.Default;
                mp.MinimumEdgeLength       = Revit.VertexTolerance;
                mp.ClosedObjectPostProcess = true;
                mp.JaggedSeams             = false;

                var brepMesh = new Mesh();
                if (Mesh.CreateFromBrep(brep, mp) is Mesh[] meshes)
                {
                    brepMesh.Append(meshes);
                }

                foreach (var g in ToGeometryObjectMany(brepMesh))
                {
                    yield return(g);
                }
            }
        }
        public static DB.GeometryObject[] ToShape(this GeometryBase geometry, double factor)
        {
            switch (geometry)
            {
            case Point point:
                return(new DB.GeometryObject[] { point.ToPoint(factor) });

            case PointCloud pointCloud:
                return(pointCloud.ToPoints(factor));

            case Curve curve:
                return(curve.ToCurveMany(factor).SelectMany(x => x.ToBoundedCurves()).OfType <DB.GeometryObject>().ToArray());

            case Brep brep:
                return(ToGeometryObjectMany(BrepEncoder.ToRawBrep(brep, factor)).OfType <DB.GeometryObject>().ToArray());

            case Extrusion extrusion:
                return(ToGeometryObjectMany(ExtrusionEncoder.ToRawBrep(extrusion, factor)).OfType <DB.GeometryObject>().ToArray());

            case SubD subD:
                return(ToGeometryObjectMany(SubDEncoder.ToRawBrep(subD, factor)).OfType <DB.GeometryObject>().ToArray());;

            case Mesh mesh:
                return(new DB.GeometryObject[] { MeshEncoder.ToMesh(MeshEncoder.ToRawMesh(mesh, factor)) });

            default:
                if (geometry.HasBrepForm)
                {
                    var brepForm = Brep.TryConvertBrep(geometry);
                    if (BrepEncoder.EncodeRaw(ref brepForm, factor))
                    {
                        return(ToGeometryObjectMany(brepForm).OfType <DB.GeometryObject>().ToArray());
                    }
                }

                return(new DB.GeometryObject[0]);
            }
        }
Beispiel #5
0
        internal static Brep ToRawBrep(/*const*/ SubD subD, double scaleFactor)
        {
            var brep = subD.ToBrep();

            return(BrepEncoder.EncodeRaw(ref brep, scaleFactor) ? brep : default);
Beispiel #6
0
 public static DB.Solid ToSolid(this Mesh value, double factor) => BrepEncoder.ToSolid(MeshEncoder.ToRawBrep(value, factor));
Beispiel #7
0
 public static DB.Solid ToSolid(this Brep value) => BrepEncoder.ToSolid(BrepEncoder.ToRawBrep(value, UnitConverter.ToHostUnits));
        internal static Brep ToRawBrep(/*const*/ Extrusion extrusion, double scaleFactor)
        {
            var brep = extrusion.ToBrep();

            return(BrepEncoder.EncodeRaw(ref brep, scaleFactor) ? brep : default);