public void BinaryReadWrite() { var originalBytes = TestData.GetTestData(@"SourceData\Brep\ImprintRingFace.brep"); Assume.That(originalBytes, Is.Not.Null); // Read in as ASCII var originalShape = BRepExchange.ReadASCII(originalBytes); Assert.IsNotNull(originalShape); Assert.AreEqual(TopAbs_ShapeEnum.TopAbs_COMPOUND, originalShape.ShapeType()); // Write out var writtenBytes = BRepExchange.WriteBinary(originalShape, false); Assert.IsNotNull(writtenBytes); Assert.AreEqual(7222, writtenBytes.Length); // Re-read in var rereadShape = BRepExchange.ReadBinary(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")); }
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")); }
//-------------------------------------------------------------------------------------------------- #endregion #region IBrepImporter public bool DoImport(string fileName, out IEnumerable <Body> bodies) { bodies = null; try { var bytes = File.ReadAllBytes(fileName); var occShape = BRepExchange.ReadASCII(bytes) ?? BRepExchange.ReadBinary(bytes); if (occShape == null) { Messages.Error("Error importing file " + fileName + "."); return(false); } // Get top level transformation for body var trsf = occShape.Location().Transformation(); var position = trsf.TranslationPart().ToPnt(); var rotation = trsf.GetRotation(); // eliminate top level transformation occShape.Location(new TopLoc_Location()); var body = Body.Create(Solid.Create(occShape)); body.Position = position; body.Rotation = rotation; body.Name = Path.GetFileNameWithoutExtension(fileName); bodies = new[] { body }; return(true); } catch (Exception e) { Console.WriteLine(e); Messages.Exception("Error importing file " + fileName + ".", e); } return(false); }
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")); }
public void BinaryTriangulation() { 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.WriteBinary(originalShape, true); Assert.IsNotNull(writtenBytes); Assert.AreEqual(1624845, writtenBytes.Length); // Re-read in with triangulation var rereadShape = BRepExchange.ReadBinary(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.WriteBinary(originalShape, false); Assert.IsNotNull(writtenBytes); Assert.AreEqual(665759, writtenBytes.Length); // Re-read in w/o triangulation rereadShape = BRepExchange.ReadBinary(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")); }
//-------------------------------------------------------------------------------------------------- #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); }
//-------------------------------------------------------------------------------------------------- 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); }