예제 #1
0
        public static bool Import(Stream stream, out IDictionary <int, Pnt2d> points, out IDictionary <int, SketchSegment> segments,
                                  DxfFlags flags = DxfFlags.None, double precision = 0.01, double scale = 1.0)
        {
            var importer = new DxfSketchImporter();

            return(importer._Import(stream, out points, out segments, flags, precision, scale));
        }
예제 #2
0
        public static MemoryStream Export(Drawing drawing,
                                          DxfVersion version, DxfFlags flags = DxfFlags.None, double precision = 0.01)
        {
            var exporter = new DxfDrawingExporter();

            return(exporter._Export(drawing, version, flags, precision));
        }
예제 #3
0
        public static MemoryStream Export(VectorExportTemplate template, VectorExportLayer[] layers,
                                          DxfVersion version, DxfFlags flags = DxfFlags.None, double precision = 0.01)
        {
            var exporter = new DxfVectorExporter();

            return(exporter._Export(template, layers, version, flags, precision));
        }
        //--------------------------------------------------------------------------------------------------

        void _ReadFromBytes(byte[] bytes, Sketch sketch, bool replace, DxfFlags flags = DxfFlags.None, double scale = 1.0)
        {
            Assert.IsTrue(DxfSketchImporter.Import(new MemoryStream(bytes), out var points, out var segments, flags, 0.01, scale));

            if (replace && points?.Count > 0)
            {
                sketch.Clear();
            }
            sketch.AddElements(points, null, segments, null);
        }
예제 #5
0
        //--------------------------------------------------------------------------------------------------

        MemoryStream _Export(Drawing drawing, DxfVersion version, DxfFlags flags, double precision)
        {
            _Precision = precision;
            _Document  = new DxfDomDocument(version, flags);

            // Export
            drawing.Render(this);

            var stream = _Document.WriteToStream();

            _Document = null;
            return(stream);
        }
예제 #6
0
        //--------------------------------------------------------------------------------------------------

        #endregion

        #region c'tor

        public DxfDomDocument(DxfVersion version = DxfVersion.Latest, DxfFlags flags = DxfFlags.None)
        {
            Version = version == DxfVersion.Latest ? DxfVersion.AC1015 : version;
            Flags   = flags;

            if (Version < DxfVersion.AC1012)
            {
                Flags = flags.Added(DxfFlags.ExportSplineAsPolygon)
                        .Added(DxfFlags.ExportEllipseAsPolygon);
            }

            Layers.Add(new DxfDomLayer("0"));
            Linetypes.Add(new DxfDomLinetype("BYBLOCK", "", 0.0, new double[0]));
            Linetypes.Add(new DxfDomLinetype("BYLAYER", "", 0.0, new double[0]));
            Linetypes.Add(new DxfDomLinetype("CONTINUOUS", "Solid line", 0.0, new double[0]));
        }
예제 #7
0
        //--------------------------------------------------------------------------------------------------

        MemoryStream _Export(VectorExportTemplate template, VectorExportLayer[] layers,
                             DxfVersion version, DxfFlags flags, double precision)
        {
            _Precision = precision;
            _Document  = new DxfDomDocument(version, flags);

            _AddLinetypes(template);

            // Export
            foreach (var layer in layers)
            {
                _ExportLayer(layer);
            }

            var stream = _Document.WriteToStream();

            _Document = null;
            return(stream);
        }
        //--------------------------------------------------------------------------------------------------

        #region Helper

        MemoryStream RunExporter(bool useTriangulation, Ax3 projection, TopoDS_Shape[] shapes,
                                 DxfVersion version = DxfVersion.Latest, DxfFlags flags = DxfFlags.None)
        {
            var helper = new DrawingExportHelper(useTriangulation, projection);

            helper.IncludeEdgeType(HlrEdgeType.VisibleSharp);
            helper.IncludeEdgeType(HlrEdgeType.VisibleOutline);
            helper.IncludeEdgeType(HlrEdgeType.VisibleSmooth);
            helper.IncludeEdgeType(HlrEdgeType.HiddenSharp);
            helper.IncludeEdgeType(HlrEdgeType.HiddenOutline);

            var layers = helper.PrepareExportLayers(shapes);

            if (layers == null || layers.Length == 0)
            {
                return(new MemoryStream("!HLRExporterError!".ToUtf8Bytes()));
            }

            return(DxfVectorExporter.Export(VectorExportTemplate.Drawing, layers, version, flags));
        }
예제 #9
0
        //--------------------------------------------------------------------------------------------------

        MemoryStream _Export(Sketch sketch, DxfVersion version, DxfFlags flags, double precision)
        {
            _Sketch    = sketch;
            _Precision = precision;
            _Document  = new DxfDomDocument(version, flags);

            foreach (var segment in _Sketch.Segments.Values)
            {
                switch (segment)
                {
                case SketchSegmentLine line:
                    _AddLineSegment(line);
                    break;

                case SketchSegmentCircle circle:
                    _AddCircleSegment(circle);
                    break;

                case SketchSegmentArc arc:
                    _AddArcSegment(arc);
                    break;

                case SketchSegmentEllipse ellipse:
                    _AddEllipseSegment(ellipse);
                    break;

                case SketchSegmentEllipticalArc ellipArc:
                    _AddEllipticalArcSegment(ellipArc);
                    break;

                case SketchSegmentBezier bezier:
                    _AddBezierSegment(bezier);
                    break;
                }
            }

            var stream = _Document.WriteToStream();

            _Document = null;
            return(stream);
        }
예제 #10
0
        //--------------------------------------------------------------------------------------------------

        #endregion

        #region Importer

        bool _Import(Stream stream, out IDictionary <int, Pnt2d> points, out IDictionary <int, SketchSegment> segments,
                     DxfFlags flags, double precision, double scale)
        {
            points   = null;
            segments = null;

            try
            {
                _MergePrecision = precision;
                _Document       = new DxfDomDocument(DxfVersion.Latest, flags);
                if (!_Document.ReadFromStream(stream, scale))
                {
                    Messages.Error("DxfImporter: The DXF document is invalid or does not contain any importable entities.");
                    _Document = null;
                    return(false);
                }

                // Iterate through children
                foreach (var entity in _Document.Entities)
                {
                    _ImportEntity(entity);
                }

                if (_Segments.Count == 0)
                {
                    Messages.Error("DxfImporter: Could not find any usable elements in DXF data.");
                    return(false);
                }

                points   = _Points;
                segments = _Segments.ToIndexedDictionary();
                return(true);
            }
            catch (Exception e)
            {
                Messages.Exception("DxfImporter: An exception occured while importing the dxf file.", e);
                return(false);
            }
        }
예제 #11
0
        public static MemoryStream Export(Sketch sketch, DxfVersion version, DxfFlags flags, double precision = 0.01)
        {
            var exporter = new DxfSketchExporter();

            return(exporter._Export(sketch, version, flags, precision));
        }
예제 #12
0
        //--------------------------------------------------------------------------------------------------

        #region Helper

        MemoryStream RunExporter(bool useTriangulation, Ax3 projection, DxfVersion version, DxfFlags flags, params Body[] bodies)
        {
            var hlrEdgeTypes = HlrEdgeTypes.VisibleSharp | HlrEdgeTypes.VisibleOutline | HlrEdgeTypes.VisibleSmooth
                               | HlrEdgeTypes.HiddenSharp | HlrEdgeTypes.HiddenOutline;

            IBrepSource[] sources        = bodies.Select(body => (IBrepSource) new BodyBrepSource(body)).ToArray();
            var           hlrBrepDrawing = HlrDrawing.Create(projection, hlrEdgeTypes, sources);

            hlrBrepDrawing.UseTriangulation = useTriangulation;

            var drawing = new Drawing();

            drawing.Add(hlrBrepDrawing);

            return(DxfDrawingExporter.Export(drawing, version, flags));
        }