Ejemplo n.º 1
0
        public void AsciiReadWrite()
        {
            var originalBytes = TestData.GetTestData(@"SourceData\Brep\ImprintRingFace.brep");

            Assume.That(originalBytes, Is.Not.Null);

            // Read in
            var originalShape = BRepExchange.ReadASCII(originalBytes);

            Assert.IsNotNull(originalShape);
            Assert.AreEqual(TopAbs_ShapeEnum.TopAbs_COMPOUND, originalShape.ShapeType());

            // Write out
            var writtenBytes = BRepExchange.WriteASCII(originalShape, false);

            Assert.IsNotNull(writtenBytes);
            Assert.AreEqual(4900, writtenBytes.Length, 50); // due to some slight differences (e.g. +/-0)

            // Re-read in
            var rereadShape = BRepExchange.ReadASCII(writtenBytes);

            Assert.IsNotNull(rereadShape);
            Assert.AreEqual(TopAbs_ShapeEnum.TopAbs_COMPOUND, rereadShape.ShapeType());
            Assert.IsFalse(_HasTriangulation(rereadShape), "HasTriangulation");

            Assert.IsTrue(ModelCompare.CompareShape(rereadShape, @"SourceData\Brep\ImprintRingFace"));
        }
Ejemplo n.º 2
0
        public void AsciiTriangulation()
        {
            var originalBytes = TestData.GetTestData(@"SourceData\Brep\Motor-c.brep");

            Assume.That(originalBytes, Is.Not.Null);

            // Read in
            var originalShape = BRepExchange.ReadASCII(originalBytes);

            Assert.IsNotNull(originalShape);
            Assert.AreEqual(TopAbs_ShapeEnum.TopAbs_COMPOUND, originalShape.ShapeType());

            // Write out with triangulation
            var writtenBytes = BRepExchange.WriteASCII(originalShape, true);

            Assert.IsNotNull(writtenBytes);
            Assert.AreEqual(2584000, writtenBytes.Length, 1000); // due to some slight differences (e.g. +/-0)

            // Re-read in with triangulation
            var rereadShape = BRepExchange.ReadASCII(writtenBytes);

            Assert.IsNotNull(rereadShape);
            Assert.AreEqual(TopAbs_ShapeEnum.TopAbs_COMPOUND, rereadShape.ShapeType());
            Assert.IsTrue(_HasTriangulation(rereadShape), "HasTriangulation");
            Assert.IsTrue(ModelCompare.CompareShape(rereadShape, @"SourceData\Brep\Motor-c"));

            // Write out w/o triangulation
            writtenBytes = BRepExchange.WriteASCII(originalShape, false);
            Assert.IsNotNull(writtenBytes);
            Assert.AreEqual(1118000, writtenBytes.Length, 1000); // due to some slight differences (e.g. +/-0)

            // Re-read in w/o triangulation
            rereadShape = BRepExchange.ReadASCII(writtenBytes);
            Assert.IsNotNull(rereadShape);
            Assert.AreEqual(TopAbs_ShapeEnum.TopAbs_COMPOUND, rereadShape.ShapeType());
            Assert.IsFalse(_HasTriangulation(rereadShape), "HasTriangulation");
            Assert.IsTrue(ModelCompare.CompareShape(rereadShape, @"SourceData\Brep\Motor-c"));
        }
Ejemplo n.º 3
0
        //--------------------------------------------------------------------------------------------------

        TopoDS_Shape _CreateProfile(TopoDS_Edge firstSpineEdge)
        {
            var firstSpineCurve = firstSpineEdge.Curve(out double firstParam, out double secondParam);

            if (firstSpineCurve == null)
            {
                return(null);
            }

            Pnt location    = default;
            Vec edgeTangent = default;

            firstSpineCurve.D1(firstParam, ref location, ref edgeTangent);
            Pln plane = new Pln(location, edgeTangent.ToDir());

            // Custom sketch
            if (_Profile == ProfileType.Custom)
            {
                return(_CreateProfileFromSketch(plane));
            }

            // Presets
            var edges = _CreateProfileEdges(plane, _SizeX * 0.5, _SizeY * 0.5);

            if (_Debug_WriteoutProfile)
            {
                TopoDS_Builder  builder  = new();
                TopoDS_Compound compound = new();
                builder.MakeCompound(compound);
                foreach (var edge in edges)
                {
                    builder.Add(compound, edge);
                }
                File.WriteAllBytes(@"_Debug_PipeProfile.brep", BRepExchange.WriteASCII(compound, false));
            }

            BRepBuilderAPI_MakeWire wireMaker = new();

            foreach (var edge in _CreateProfileEdges(plane, _SizeX * 0.5, _SizeY * 0.5))
            {
                wireMaker.Add(edge);
            }
            TopoDS_Shape result = wireMaker.Wire();

            if (_Profile == ProfileType.HollowCircle ||
                _Profile == ProfileType.HollowRectangle)
            {
                var innerSizeX = _SizeX * 0.5 - _Thickness;
                var innerSizeY = _SizeY * 0.5 - _Thickness;
                if (innerSizeX > 0 && (_Flags.HasFlag(PipeFlags.SymmetricProfile) || innerSizeY > 0))
                {
                    TopoDS_Builder  builder  = new();
                    TopoDS_Compound compound = new();
                    builder.MakeCompound(compound);
                    builder.Add(compound, result);

                    wireMaker = new();
                    foreach (var edge in _CreateProfileEdges(plane, innerSizeX, innerSizeY))
                    {
                        wireMaker.Add(edge);
                    }
                    builder.Add(compound, wireMaker.Wire());
                    result = compound;
                }
            }

            var face = TopoUtils.CreateFacesFromWires(result, plane);

            return(face);
        }
Ejemplo n.º 4
0
        //--------------------------------------------------------------------------------------------------

        #endregion

        #region IBrepExporter

        bool IBodyExporter.DoExport(string fileName, IEnumerable <Body> bodies)
        {
            try
            {
                var builder  = new BRep_Builder();
                var compound = new TopoDS_Compound();
                builder.MakeCompound(compound);
                foreach (var body in bodies)
                {
                    var bodyShape = body.Shape?.GetTransformedBRep();
                    if (bodyShape == null)
                    {
                        Messages.Warning($"BRep Exporter: The body {body.Name} has no valid shape, thus it will not be included in the export.");
                        continue;
                    }

                    builder.Add(compound, bodyShape);
                }

                var bytes = Settings.ExportBinaryFormat ? BRepExchange.WriteBinary(compound, true) : BRepExchange.WriteASCII(compound, true);
                if (bytes == null || bytes.Length == 0)
                {
                    Messages.Error("BRep Exporter: Error generating BRep from body shapes.");
                    return(false);
                }

                File.WriteAllBytes(fileName, bytes);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Messages.Exception("Error exporting file " + fileName + ".", e);
            }

            return(false);
        }