def create_doc(filename, content_creator): print(f"start DXF document: {filename}") doc = ezdxf.new(dxfversion='R2004') for name in LAYERS: doc.layers.new(name) doc.styles.new(ATTRIBS, dxfattribs={'font': 'OpenSansCondensed-Light.ttf'}) content_creator(doc) msp = doc.modelspace() print("exploding ...") ts = perf_counter() explode(msp) print(f"... required {perf_counter()-ts:.2f}s") print("zooming ...") ts = perf_counter() if EXPLODE_CONTENT: # processing only LINE entities is much faster: zoom.objects(msp, doc.modelspace().query('LINE')) else: zoom.extents(msp) print(f"... required {perf_counter()-ts:.2f}s") print("saving ...") ts = perf_counter() doc.saveas(filename) print(f"... required {perf_counter()-ts:.2f}s") print(f"saved DXF document: {filename}\n")
def test_zoom_objects_of_empty_set(self, msp): vp = msp.doc.viewports.get('*Active')[0] old_center = vp.dxf.center old_height = vp.dxf.height zoom.objects(msp, []) vp = msp.doc.viewports.get('*Active')[0] assert vp.dxf.center == old_center assert vp.dxf.height == old_height
def test_zoom_objects_of_empty_set(self, psp): vp = psp.main_viewport() old_center = vp.dxf.center old_height = vp.dxf.height zoom.objects(psp, []) vp = psp.main_viewport() assert vp.dxf.center == old_center assert vp.dxf.height == old_height
def test_zoom_objects_but_ignore_main_viewport(self, psp): vp = psp.main_viewport() # Clipping path of viewports are taken into account, but the main # viewport is ignored: objects = [psp[0], vp] zoom.objects(psp, objects) assert vp.dxf.center == (40, 40) assert vp.dxf.width == 40 assert vp.dxf.height == 40
def test_zoom_objects(self, msp): points = msp[:2] zoom.objects(msp, points) vp = msp.doc.viewports.get('*Active')[0] assert vp.dxf.center == (0, 0) assert vp.dxf.height == 50
user_record.commit() # ------------------------------------------------------------------------------ # Example 3: Store arbitrary binary data my_dict = doc.rootdict.get_required_dict("MyDict") xrecord = my_dict.add_xrecord("MyBinaryData") with BinaryRecord(xrecord) as binary_record: # The content is stored as hex strings (e.g. ABBAFEFE...) in one or more # group code 310 tags. # A preceding group code 160 tag stores the data size in bytes. data = b"Store any binary data, even line breaks\r\n" * 20 # compress data if required binary_record.data = zlib.compress(data, level=9) zoom.objects(msp, [gear_]) doc.saveas(DIR / "gear_with_user_data.dxf") # ------------------------------------------------------------------------------ # Retrieving data from reloaded DXF file # ------------------------------------------------------------------------------ doc = ezdxf.readfile(DIR / "gear_with_user_data.dxf") gear_ = doc.entitydb.get(handle) entity: Optional[DXFEntity] # ------------------------------------------------------------------------------ # Example 1: Get entity specific data from the associated extension dict print("\nContent of example 1:") if gear_.has_extension_dict:
FILE1 = "text_mirror_true_type_font.dxf" FILE2 = "text_oblique_rotate.dxf" FILE = FILE2 doc = ezdxf.readfile(EXAMPLES / FILE) doc.layers.new('OUTLINE', dxfattribs={'color': 1}) doc.layers.new('BBOX', dxfattribs={'color': 5}) msp = doc.modelspace() text_entities = msp.query('TEXT') # Convert TEXT entities into SPLINE and POLYLINE entities: kind = text2path.Kind.SPLINES for text in text_entities: for e in text2path.virtual_entities(text, kind=kind): e.dxf.layer = 'OUTLINE' e.dxf.color = const.BYLAYER msp.add_entity(e) # Add bounding boxes attrib = {'layer': 'BBOX'} boxes = [] # The "primitive" representation for TEXT entities is the bounding box: for prim in disassemble.to_primitives(text_entities): p = msp.add_lwpolyline(prim.vertices(), dxfattribs=attrib) boxes.append(p) # Zoom on bounding boxes (fast): zoom.objects(msp, boxes,factor=1.1) doc.saveas(OUTBOX / FILE)
arc_tool = ConstructionArc.from_3p(start, end, middle) arc_tool.add_to_layout(msp, dxfattribs={ "layer": "recreated arc", "color": ezdxf.const.MAGENTA, }) # This only works for flat B-splines in the xy-plane, a.k.a. 2D splines! # Check the assumption: center = arc_tool.center radius = arc_tool.radius err = max(abs(radius - p.distance(center)) for p in (chk1, chk2)) print(f"max error: {err:.6f}") # Warning: this does not proof that the assumption was correct, it is always # possible to create a diverging B-spline which matches the check points: foul = (4.5, 1) fit_points = [start, foul, chk1, middle, chk2, end] msp.add_spline(fit_points, dxfattribs={ "layer": "foul B-spline", "color": ezdxf.const.RED, }) # add check marks for p in fit_points: msp.add_circle(p, radius=0.03, dxfattribs={"color": ezdxf.const.RED}) zoom.objects(msp, [arc]) doc.saveas(DIR / "arc_recreation.dxf")
user_record = UserRecord(entity) pprint(user_record.data) # ------------------------------------------------------------------------------ # Example 3: Store arbitrary binary data my_dict = doc.rootdict.get_required_dict("MyDict") xrecord = my_dict.add_xrecord("MyBinaryData") with BinaryRecord(xrecord) as binary_record: # The content is stored as hex strings (e.g. ABBAFEFE...) in one or more # group code 310 tags. # A preceding group code 160 tag stores the data size in bytes. data = b"Store any binary data, even line breaks\r\n" * 20 # compress data if required binary_record.data = zlib.compress(data, level=9) zoom.objects(msp, [line]) doc.saveas(DIR / "gear_with_user_data.dxf") # ------------------------------------------------------------------------------ # Example 3: Get binary data from DICTIONARY object entity = my_dict["MyBinaryData"] if isinstance(entity, XRecord): binary_record = BinaryRecord(entity) print("\ncompressed data:") pprint(binary_record.data) print("\nuncompressed data:") pprint(zlib.decompress(binary_record.data))