📄 ezdxfimport.pas
字号:
Unit EzDxfImport;
{***********************************************************}
{ EzGIS/CAD Components }
{ (c) 2003 EzSoft Engineering }
{ All Rights Reserved }
{***********************************************************}
{$I EZ_FLAG.PAS}
Interface
Uses
Windows, Classes, Graphics, SysUtils, Dialogs, Forms,
EzDXFUtil, Controls,
EzBase, EzBaseGIS, EzEntities, EzLib, EzSystem, EzImportBase, Math, EzCadCtrls;
Const
// AutoCAD places a limit on this, but I don't know what it is
max_vertices_per_polyline = 8192;
MaxInt = High( Integer );
MinInt = Low( Integer );
max_attribs = 16; // I don't know what it is...
max_my_attribs = 16;
Type
Dxf_Object = Class; // forward declare
{ TEzDxfFile }
TEzDxfFile = Class( TComponent )
Private
FFileName: String;
FDrawBox: TEzBaseDrawBox;
FDxf_Main: Dxf_Object;
FOnFileProgress: TEzFileProgressEvent;
Procedure SetDrawBox( Const Value: TEzBaseDrawBox );
function GetAbout: TEzAbout;
procedure SetAbout(const Value: TEzAbout);
Protected
Procedure Notification( AComponent: TComponent; Operation: TOperation ); Override;
Public
Destructor Destroy; Override;
Property Dxf_Main: Dxf_Object Read FDxf_Main;
Function ProperColor( clr: TColor ): TColor;
Published
{ properties }
Property FileName: String Read FFileName Write FFileName;
Property DrawBox: TEzBaseDrawBox Read FDrawBox Write SetDrawBox;
{ events }
Property OnFileProgress: TEzFileProgressEvent Read FOnFileProgress Write FOnFileProgress;
Property About: TEzAbout read GetAbout write SetAbout;
End;
{ TEzDxfImport }
TEzDxfImport = Class( TEzDxfFile )
Private
FConverter: TEzImportConverter;
FMustDeleteConverter: Boolean;
FLayerName: String; // LAYERNAME TO IMPORT/EXPORT
FConfirmProjectionSystem: Boolean;
FDxf_Emin: Point3D;
FDxf_Emax: Point3D;
FLayer: TEzBaseLayer;
FDxfReferenceX: Double;
FDxfReferenceY: Double;
FDestReferenceX: Double;
FDestReferenceY: Double;
FImportLayerList: TStrings;
FFullLayerList: TStrings;
FTotalEntities: Integer;
FTotalProcessed: Integer;
FMsg: String;
FExplodeBlocks: Boolean;
FUseTrueType: Boolean;
FCad: TEzCAD;
FGenerateMultiLayers: Boolean;
FTargetNames: TStrings;
FImportBlocks: Boolean;
FOnBeforeImport: TNotifyEvent;
FOnAfterImport: TNotifyEvent;
Procedure SetLayerName( Const Value: String );
Procedure SetConverter( Value: TEzImportConverter );
Function UpdateProgress: Boolean;
{$IFDEF BCB}
function GetConfirmProjectionSystem: Boolean;
function GetDestReferenceX: Double;
function GetDestReferenceY: Double;
function GetDxf_Emax: Point3D;
function GetDxf_Emin: Point3D;
function GetDxfReferenceX: Double;
function GetDxfReferenceY: Double;
function GetExplodeBlocks: Boolean;
function GetFullLayerList: TStrings;
function GetImportLayerList: TStrings;
function GetLayer: TEzBaseLayer;
function GetLayerName: String;
function GetOnAfterImport: TNotifyEvent;
function GetOnBeforeImport: TNotifyEvent;
function GetUseTrueType: Boolean;
procedure SetConfirmProjectionSystem(const Value: Boolean);
procedure SetDestReferenceX(const Value: Double);
procedure SetDestReferenceY(const Value: Double);
procedure SetDxf_Emax(const Value: Point3D);
procedure SetDxf_Emin(const Value: Point3D);
procedure SetDxfReferenceX(const Value: Double);
procedure SetDxfReferenceY(const Value: Double);
procedure SetExplodeBlocks(const Value: Boolean);
{$ENDIF}
Public
Constructor Create( AOwner: TComponent ); Override;
Destructor Destroy; Override;
Function ReadDxf: Boolean;
Function Execute: Boolean;
Function ImportFiles( FileList: TStrings ): Boolean;
Property ImportLayerList: TStrings {$IFDEF BCB} Read GetImportLayerList {$ELSE} Read FImportLayerList {$ENDIF};
Property FullLayerList: TStrings {$IFDEF BCB} Read GetFullLayerList {$ELSE} Read FFullLayerList{$ENDIF};
Property Layer: TEzBaseLayer {$IFDEF BCB} Read GetLayer {$ELSE} Read FLayer{$ENDIF};
Property Dxf_Emin: Point3D {$IFDEF BCB} Read GetDxf_Emin Write SetDxf_Emin {$ELSE} Read FDxf_Emin Write FDxf_Emin{$ENDIF};
Property Dxf_Emax: Point3D {$IFDEF BCB} Read GetDxf_Emax Write SetDxf_Emax {$ELSE} Read FDxf_Emax Write FDxf_Emax{$ENDIF};
Property Converter: TEzImportConverter {$IFDEF BCB} Read FConverter Write SetConverter {$ELSE} Read FConverter Write SetConverter{$ENDIF};
Property Cad: TEzCAD read FCad;
Property TargetNames: TStrings read FTargetNames;
Property GenerateMultiLayers: Boolean read FGenerateMultiLayers write FGenerateMultiLayers;
Published
Property LayerName: String {$IFDEF BCB} Read GetLayerName Write SetLayerName {$ELSE} Read FLayerName Write SetLayerName{$ENDIF};
Property ConfirmProjectionSystem: Boolean {$IFDEF BCB} Read GetConfirmProjectionSystem Write SetConfirmProjectionSystem {$ELSE} Read FConfirmProjectionSystem Write FConfirmProjectionSystem{$ENDIF};
Property DxfReferenceX: Double {$IFDEF BCB} Read GetDxfReferenceX Write SetDxfReferenceX {$ELSE} Read FDxfReferenceX Write FDxfReferenceX{$ENDIF};
Property DxfReferenceY: Double {$IFDEF BCB} Read GetDxfReferenceY Write SetDxfReferenceY{$ELSE} Read FDxfReferenceY Write FDxfReferenceY{$ENDIF};
Property DestReferenceX: Double {$IFDEF BCB} Read GetDestReferenceX Write SetDestReferenceX {$ELSE} Read FDestReferenceX Write FDestReferenceX{$ENDIF};
Property DestReferenceY: Double {$IFDEF BCB} Read GetDestReferenceY Write SetDestReferenceY {$ELSE} Read FDestReferenceY Write FDestReferenceY{$ENDIF};
Property ExplodeBlocks: Boolean {$IFDEF BCB} Read GetExplodeBlocks Write SetExplodeBlocks {$ELSE} Read FExplodeBlocks Write FExplodeBlocks{$ENDIF};
Property UseTrueType: Boolean {$IFDEF BCB} read GetUseTrueType write FUseTrueType {$ELSE} read FUseTrueType write FUseTrueType{$ENDIF};
Property ImportBlocks: Boolean read FImportBlocks write FImportBlocks default True;
{ events }
Property OnBeforeImport: TNotifyEvent {$IFDEF BCB} Read GetOnBeforeImport Write FOnBeforeImport {$ELSE} Read FOnBeforeImport Write FOnBeforeImport{$ENDIF};
Property OnAfterImport: TNotifyEvent {$IFDEF BCB} Read GetOnAfterImport Write FOnAfterImport{$ELSE} Read FOnAfterImport Write FOnAfterImport{$ENDIF};
End;
{ TEzDxfExport }
TEzDxfExport = Class( TEzDxfFile )
Private
FLayerNames: TStrings;
Procedure SetLayerNames( Const Value: TStrings );
{$IFDEF BCB}
function GetLayerNames: TStrings;
{$ENDIF}
Public
Constructor Create( AOwner: TComponent ); Override;
Destructor Destroy; Override;
Function Execute: Boolean;
Published
Property LayerNames: TStrings {$IFDEF BCB} Read GetLayerNames {$ELSE} Read FLayerNames {$ENDIF} Write SetLayerNames;
End;
{ start of original dxf_stru unit }
polyface = Record
nf: Array[0..3] Of integer;
End;
pfacelist = ^facelist;
facelist = Array[0..0] Of polyface;
pintlist = ^intlist;
intlist = Array[0..0] Of integer;
pattrlist = ^attrlist;
attrlist = Array[0..0] Of Extended;
// note the addition of base and scale factor for drawing blocks
coord_convert = Function( P: Point3D; OCS: pMatrix ): TPoint Of Object;
coord_convert3D = Function( P: Point3D; OCS: pMatrix ): Point3D Of Object;
planar_eq = Record
a, b, c, d: Double;
End;
///////////////////////////////////////////////////////////////////////////////
// DXF_Entity - abstract base class - override where neccessary
// All DXF objects will become sub classes of this
///////////////////////////////////////////////////////////////////////////////
// Added for GIS
TEllipse2DClass = Class( TEzEllipse );
//TText2DClass = class(TText2D);
DXF_Entity = Class
colour: TColor;
colinx: integer;
OCS_WCS: pMatrix;
OCS_axis: Point3D;
public
//Constructor Create;
Destructor Destroy; Override;
Procedure init_OCS_WCS_matrix( OCSaxis: Point3D ); Virtual;
Procedure update_block_links( blist: TObject ); Virtual;
Procedure setcolour_index( col: integer ); Virtual;
Procedure setcolour( col: TColor ); Virtual;
Procedure translate( T: Point3D ); Virtual;
Procedure quantize_coords( epsilon: Double; mask: byte ); Virtual;
Function count_points: integer; Virtual;
Function count_lines: integer; Virtual;
Function count_polys_open: integer; Virtual;
Function count_polys_closed: integer; Virtual;
Function proper_name: String; Virtual;
Procedure write_DXF_Point( Var IO: textfile; n: integer; p: Point3D ); Virtual;
Procedure write_to_DXF( Var IO: textfile; Const layer: String; Lcolor: integer ); Virtual;
Procedure max_min_extents( Var emax, emin: Point3D ); Virtual;
Function closest_vertex_square_distance_2D( p: Point3D ): Double; Virtual;
Function closest_vertex( p: Point3D ): Point3D; Virtual;
Function is_point_inside_object2D( p: Point3D ): boolean; Virtual;
Function Move_point( const p, newpoint: Point3D ): boolean; Virtual;
// Added for GIS
Procedure AddToGIS( DxfImport: TEzDxfImport; OCS: pM ); Virtual;
End;
///////////////////////////////////////////////////////////////////////////////
// Block_ Definition - special case - not to be used like other entities
// Blocks should always appear in layer 'Block_'
// I'm still not quite sure what to do with them - but here goes anyway...
///////////////////////////////////////////////////////////////////////////////
Block_ = Class( DXF_Entity )
rotation: Double;
name: String;
basepoint: Point3D;
entities: TList;
NoOCS_WCS: pMatrix;
Constructor Create( Const bname: String; refpoint: Point3D );
Destructor Destroy; Override;
Procedure update_block_links( blist: TObject ); Override;
Procedure write_to_DXF( Var IO: textfile; Const layer: String; Lcolor: integer ); Override;
Procedure max_min_extents( Var emax, emin: Point3D ); Override;
Function closest_vertex_square_distance_2D( p: Point3D ): Double; Override;
Function closest_vertex( p: Point3D ): Point3D; Override;
Procedure setcolour( col: TColor ); Override;
// Procedure CreateCopy(var Result : Block_) ;
// Added for GIS
Procedure AddToGIS( DxfImport: TEzDxfImport; OCS: pM ); Override;
End;
///////////////////////////////////////////////////////////////////////////////
// Point Definition
///////////////////////////////////////////////////////////////////////////////
Point_ = Class( DXF_Entity ) // always WCS
p1: Point3D;
LineStyle: integer;
Constructor Create( OCSaxis, p: Point3D; col: integer );
Procedure translate( T: Point3D ); Override;
Procedure quantize_coords( epsilon: Double; mask: byte ); Override;
Procedure write_to_DXF( Var IO: textfile; Const layer: String; Lcolor: integer ); Override;
Procedure max_min_extents( Var emax, emin: Point3D ); Override;
Function closest_vertex_square_distance_2D( p: Point3D ): Double; Override;
Function closest_vertex( p: Point3D ): Point3D; Override;
Function Move_point( const p, newpoint: Point3D ): boolean; Override;
// Added for GIS
Procedure AddToGIS( DxfImport: TEzDxfImport; OCS: pM ); Override;
End;
///////////////////////////////////////////////////////////////////////////////
// Text Definition
///////////////////////////////////////////////////////////////////////////////
Text_ = Class( Point_ ) // always OCS
h: Double;
textstr: String;
style: String;
align_pt: Point3D; // alignment point
hor_align: integer; // horizontal justification code
rotation: Double;
Constructor Create( OCSaxis, p, ap: Point3D; rot: Double; Const ss, s1: String; height: Double; col, ha: integer );
Procedure write_to_DXF( Var IO: textfile; Const layer: String; Lcolor: integer ); Override;
Procedure max_min_extents( Var emax, emin: Point3D ); Override;
// Added for GIS
Procedure AddToGIS( DxfImport: TEzDxfImport; OCS: pM ); Override;
End;
///////////////////////////////////////////////////////////////////////////////
// Attrib Definition
///////////////////////////////////////////////////////////////////////////////
Attrib_ = Class( Text_ ) // always OCS
tagstr: String;
visible: boolean;
Constructor Create( OCSaxis, p, ap: Point3D; rot: Double; Const ss, s1, tag: String; flag70, flag72: integer; height:
Double; col: integer );
Procedure write_to_DXF( Var IO: textfile; Const layer: String; Lcolor: integer ); Override;
// Added for GIS
Procedure AddToGIS( DxfImport: TEzDxfImport; OCS: pM ); Override;
End;
patt_array = ^att_array;
att_array = Array[0..0] Of Attrib_;
///////////////////////////////////////////////////////////////////////////////
// Attdef Definition
///////////////////////////////////////////////////////////////////////////////
Attdef_ = Class( Attrib_ ) // always OCS
promptstr: String;
Constructor Create( OCSaxis, p, ap: Point3D; rot: Double; Const ss, s1, tag, prompt: String; flag70, flag72: integer;
height: Double; col: integer );
Procedure write_to_DXF( Var IO: textfile; Const layer: String; Lcolor: integer ); Override;
// Added for GIS
Procedure AddToGIS( DxfImport: TEzDxfImport; OCS: pM ); Override;
End;
///////////////////////////////////////////////////////////////////////////////
// Insert Definition (optionally contains attribs)
///////////////////////////////////////////////////////////////////////////////
Insert_ = Class( Point_ ) // always OCS
num_attribs: integer;
attribs: Array[0..max_attribs] Of Attrib_;
blockname: String;
scale: Point3D;
rotation: Double;
blockptr: Block_; // use carefully
blocklist: TObject; // to cross reference the blocks
NoOCS_WCS: pMatrix; //雀傈救窍绰 OCS/WCS
Constructor Create( OCSaxis, p, s_f: Point3D; rot: Double; col: integer;
numatts: integer; atts: patt_array; Const block: String );
Destructor Destroy; Override;
Procedure init_OCS_WCS_matrix( OCSaxis: Point3D ); Override;
Procedure update_block_links( blist: TObject ); Override;
Function block: Block_;
Procedure write_to_DXF( Var IO: textfile; Const layer: String; Lcolor: integer ); Override;
Procedure max_min_extents( Var emax, emin: Point3D ); Override;
// Added for GIS
Procedure AddToGIS( DxfImport: TEzDxfImport; OCS: pM ); Override;
End;
///////////////////////////////////////////////////////////////////////////////
// Line Definition
///////////////////////////////////////////////////////////////////////////////
Line_ = Class( Point_ ) // always WCS
p2: Point3D;
Constructor Create( p_1, p_2: Point3D; col: integer; LS: integer );
Procedure translate( T: Point3D ); Override;
Procedure quantize_coords( epsilon: Double; mask: byte ); Override;
Function count_points: integer; Override;
Function count_lines: integer; Override;
Procedure write_to_DXF( Var IO: textfile; Const layer: String; Lcolor: integer ); Override;
Procedure max_min_extents( Var emax, emin: Point3D ); Override;
Function closest_vertex_square_distance_2D( p: Point3D ): Double; Override;
Function closest_vertex( p: Point3D ): Point3D; Override;
Function Move_point( const p, newpoint: Point3D ): boolean; Override;
// Added for GIS
Procedure AddToGIS( DxfImport: TEzDxfImport; OCS: pM ); Override;
End;
///////////////////////////////////////////////////////////////////////////////
// Circle Definition
///////////////////////////////////////////////////////////////////////////////
Circle_ = Class( Point_ ) // always OCS
radius: Double;
Constructor Create( OCSaxis, p_1: Point3D; radius_: Double; col: integer; LS: integer );
Destructor Destroy; Override;
Constructor Create_from_polyline( ent1: DXF_Entity );
Procedure write_to_DXF( Var IO: textfile; Const layer: String; Lcolor: integer ); Override;
Function is_point_inside_object2D( p: Point3D ): boolean; Override;
Procedure max_min_extents( Var emax, emin: Point3D ); Override;
// Added for GIS
Procedure AddToGIS( DxfImport: TEzDxfImport; OCS: pM ); Override;
End;
///////////////////////////////////////////////////////////////////////////////
// Arc Definition
///////////////////////////////////////////////////////////////////////////////
Arc_ = Class( Circle_ ) // always OCS
angle1, angle2: Double;
Constructor Create( OCSaxis, p_1: Point3D; radius_, sa, ea: Double; col: integer; LS: integer );
Procedure write_to_DXF( Var IO: textfile; Const layer: String; Lcolor: integer ); Override;
Function is_point_inside_object2D( p: Point3D ): boolean; Override;
Procedure max_min_extents( Var emax, emin: Point3D ); Override;
// Added for GIS
Procedure AddToGIS( DxfImport: TEzDxfImport; OCS: pM ); Override;
End;
///////////////////////////////////////////////////////////////////////////////
// Polyline Definition
///////////////////////////////////////////////////////////////////////////////
Polyline_ = Class( DXF_Entity ) // OCS/WCS depends
closed: boolean;
numvertices: integer;
numtext: integer;
polypoints: ppointlist;
numattrs: integer;
attribs: Array[0..max_my_attribs - 1] Of Double;
linestyle: integer;
Constructor Create( const OCSaxis: Point3D; numpoints: integer; points: ppointlist;
col: integer; closed_: boolean; LS: integer );
Destructor Destroy; Override;
Procedure translate( T: Point3D ); Override;
Procedure quantize_coords( epsilon: Double; mask: byte ); Override;
Function count_points: integer; Override;
Function count_lines: integer; Override;
Function count_polys_open: integer; Override;
Function count_polys_closed: integer; Override;
Procedure write_to_DXF( Var IO: textfile; Const layer: String; Lcolor: integer ); Override;
Procedure max_min_extents( Var emax, emin: Point3D ); Override;
Function closest_vertex_square_distance_2D( p: Point3D ): Double; Override;
Function closest_vertex( p: Point3D ): Point3D; Override;
// some functions I use...most removed....
Function Move_point( const p, newpoint: Point3D ): boolean; Override;
Function is_point_inside_object2D( p: Point3D ): boolean; Override;
Function triangle_centre: Point3D;
Procedure set_attrib( i: integer; v: Double );
Function get_attrib( i: integer ): Double;
Procedure copy_attribs( p: Polyline_ );
// Added for GIS
Procedure AddToGIS( DxfImport: TEzDxfImport; OCS: pM ); Override;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -