-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdwgproc.pp
More file actions
1354 lines (1237 loc) · 45.2 KB
/
Copy pathdwgproc.pp
File metadata and controls
1354 lines (1237 loc) · 45.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{*************************************************************************** }
{ gfdwg - free implementation of the DWG file format based on LibreDWG }
{ }
{ Copyright (C) 2022 Andrey Zubarev <zamtmn@yandex.ru> }
{ }
{ This library is free software, licensed under the terms of the GNU }
{ General Public License as published by the Free Software Foundation, }
{ either version 3 of the License, or (at your option) any later version. }
{ You should have received a copy of the GNU General Public License }
{ along with this program. If not, see <http://www.gnu.org/licenses/>. }
{*************************************************************************** }
unit dwgproc;
{$IFDEF FPC}
{$PACKRECORDS C}
{$MACRO ON}
{$IFDEF Windows}
{$DEFINE extdecl := stdcall}
{$ELSE}
{$DEFINE extdecl := cdecl}
{$ENDIF}
{$Mode objfpc}{$H+}
{$ModeSwitch advancedrecords}
{$ENDIF}
interface
uses
SysUtils, Math, {ctypes,} dynlibs, dwg, ghashmap, TypInfo,
// R3: handle / text helpers moved into their own units. Re-exported so
// existing callers (uzefflibredwg2ents.pas, uzedwgtestdwgproc.pas) keep
// seeing the same names through `uses dwgproc`.
uzedwghandle, uzedwgtext;
resourcestring
rsHandlerAlreadyReg='Handler already registered for %d';
rsCouldNotLoadLib='Could not load library: %s';
const
{$if defined(Windows)}
LibreDWG_Lib = 'libredwg-0.dll';
{$elseif defined(OS2)}
//LibreDWG_Lib = '';
{$elseif defined(darwin)}
//LibreDWG_LIB = '';
{$elseif defined(haiku) or defined(OpenBSD)}
//LibreDWG_LIB = '';
{$elseif defined(MorphOS)}
//LibreDWG_LIB = '';
{$else}
LibreDWG_LIB = 'libredwg.so';
{$endif}
type
TDWGCtx=record
DWG:Dwg_Data;
DWGVer:DWG_VERSION_TYPE;
DWGCodePage:Integer;
procedure CreateRec(var ADWG:Dwg_Data);
end;
// Plain record used by ZCAD LINE mapper and its unit tests. It exposes
// the bare (x,y,z) pairs that GDBObjLine needs without dragging the
// ZCAD entity unit into dwgproc.
TDWGLineEndpoints=record
StartX,StartY,StartZ:double;
EndX,EndY,EndZ:double;
end;
// Stage 5 (TZ §12.5): plain mirror records that LINE/CIRCLE/ARC/POINT/
// LWPOLYLINE/TEXT/MTEXT mappers fill before pushing into the ZCAD entity.
// Keeping them in dwgproc means the scalar copy path is unit-testable
// against fake LibreDWG records without pulling in the zengine entity
// graph or libredwg.so.
TDWGCircleProps=record
CenterX,CenterY,CenterZ:double;
Radius:double;
Thickness:double;
end;
TDWGArcProps=record
CenterX,CenterY,CenterZ:double;
Radius:double;
Thickness:double;
StartAngle:double;
EndAngle:double;
end;
TDWGPointProps=record
X,Y,Z:double;
Thickness:double;
XAngle:double;
end;
TDWGTextProps=record
DataFlags:integer;
InsertX,InsertY,InsertZ:double;
AlignX,AlignY:double;
Height:double;
Rotation:double;
Oblique:double;
WidthFactor:double;
Generation:integer;
HorizAlignment:integer;
VertAlignment:integer;
Value:string;
end;
TDWGTextJustifyKind=(dwtjTopLeft,dwtjTopCenter,dwtjTopRight,
dwtjMiddleLeft,dwtjMiddleCenter,dwtjMiddleRight,
dwtjBottomLeft,dwtjBottomCenter,dwtjBottomRight,
dwtjLeft,dwtjCenter,dwtjRight);
TDWGMTextProps=record
InsertX,InsertY,InsertZ:double;
XAxisX,XAxisY,XAxisZ:double;
Rotation:double;
RectWidth,RectHeight:double;
TextHeight:double;
Attachment:integer;
LineSpaceFactor:double;
Value:string;
end;
TDWGMTextJustify=(dwgmtjTopLeft,dwgmtjTopCenter,dwgmtjTopRight,
dwgmtjMiddleLeft,dwgmtjMiddleCenter,dwgmtjMiddleRight,
dwgmtjBottomLeft,dwgmtjBottomCenter,dwgmtjBottomRight);
TDWGLWPolylineVertex=record
X,Y:double;
StartWidth,EndWidth,Bulge:double;
end;
TDWGLWPolylineProps=record
Closed:Boolean;
ConstWidth:double;
Elevation:double;
Thickness:double;
Vertices:array of TDWGLWPolylineVertex;
end;
TDWGProxyEntityPayload=record
ProxyID:Integer;
ClassID:Integer;
DWGVersions:Integer;
MaintVersion:Integer;
DWGVersion:Integer;
FromDXF:Integer;
EntityDataSize:Integer;
HasGraphic:Boolean;
Graphic:TBytes;
end;
// Stage 8 (TZ §12.8): raw-geometry mirror records for HATCH/SPLINE/
// ELLIPSE/SOLID/3DFACE/POLYLINE variants. They intentionally stay as
// plain records so unit tests can exercise the LibreDWG field mapping
// without allocating ZCAD entities.
TDWGPoint2D=record
X,Y:double;
end;
TDWGPoint3D=record
X,Y,Z:double;
end;
TDWGInsertProps=record
InsertPoint:TDWGPoint3D;
Scale:TDWGPoint3D;
Rotation:double;
Extrusion:TDWGPoint3D;
end;
TDWG3DFaceProps=record
Corners:array[0..3] of TDWGPoint3D;
InvisibleFlags:Integer;
end;
TDWGSolidProps=record
Thickness:double;
Elevation:double;
Corners:array[0..3] of TDWGPoint3D;
Extrusion:TDWGPoint3D;
end;
TDWGEllipseProps=record
Center:TDWGPoint3D;
MajorAxis:TDWGPoint3D;
Extrusion:TDWGPoint3D;
AxisRatio:double;
StartAngle:double;
EndAngle:double;
end;
TDWGSplineControlPoint=record
X,Y,Z,W:double;
end;
TDWGSplineProps=record
Flag:Integer;
Scenario:Integer;
Degree:Integer;
Closed:Boolean;
Periodic:Boolean;
Rational:Boolean;
Weighted:Boolean;
Knots:array of double;
ControlPoints:array of TDWGSplineControlPoint;
FitPoints:array of TDWGPoint3D;
end;
TDWGHatchPolylinePoint=record
X,Y:double;
Bulge:double;
end;
TDWGHatchPathProps=record
IsPolyline:Boolean;
Closed:Boolean;
PolylinePoints:array of TDWGHatchPolylinePoint;
end;
TDWGHatchPatternLineProps=record
Angle:double;
Base,Offset:TDWGPoint2D;
Dashes:array of double;
end;
TDWGHatchProps=record
PatternName:string;
Elevation:double;
Extrusion:TDWGPoint3D;
IsSolidFill:Boolean;
Style:Integer;
PatternType:Integer;
Angle:double;
Scale:double;
PatternLines:array of TDWGHatchPatternLineProps;
Paths:array of TDWGHatchPathProps;
end;
TDWGPolylineRefProps=record
Closed:Boolean;
Elevation:double;
VertexHandles:array of QWord;
end;
// Stage 5 (TZ §12.5): the LibreDWG bindings only expose
// PDwg_Entity_LINE; declare the pointer aliases the Stage 5 helpers
// require so callers can pass &dwg.tio.entity^.tio.TEXT directly.
// R3: PDwg_Entity_TEXT / PDwg_Entity_MTEXT are also declared in
// uzedwghandle (which is re-exported above) and reach callers from
// there; the rest stay here next to their TDWGCopy* mappers.
PDwg_Entity_CIRCLE=^Dwg_Entity_CIRCLE;
PDwg_Entity_ARC=^Dwg_Entity_ARC;
PDwg_Entity_POINT=^Dwg_Entity_POINT;
PDwg_Entity_LWPOLYLINE=^Dwg_Entity_LWPOLYLINE;
PDwg_Entity_PROXY_ENTITY=^Dwg_Entity_PROXY_ENTITY;
PDwg_Entity__3DFACE=^Dwg_Entity__3DFACE;
PDwg_Entity_SOLID=^Dwg_Entity_SOLID;
PDwg_Entity_ELLIPSE=^Dwg_Entity_ELLIPSE;
PDwg_Entity_SPLINE=^Dwg_Entity_SPLINE;
PDwg_Entity_HATCH=^Dwg_Entity_HATCH;
PDwg_Entity_POLYLINE_2D=^Dwg_Entity_POLYLINE_2D;
PDwg_Entity_POLYLINE_3D=^Dwg_Entity_POLYLINE_3D;
PDwg_Entity_POLYLINE_MESH=^Dwg_Entity_POLYLINE_MESH;
PDwg_Entity_POLYLINE_PFACE=^Dwg_Entity_POLYLINE_PFACE;
PDwg_Entity_VERTEX_2D=^Dwg_Entity_VERTEX_2D;
PDwg_Entity_VERTEX_3D=^Dwg_Entity_VERTEX_3D;
PDwg_Entity_VERTEX_MESH=^Dwg_Entity_VERTEX_MESH;
PDwg_Entity_VERTEX_PFACE=^Dwg_Entity_VERTEX_PFACE;
PDwg_Entity_VERTEX_PFACE_FACE=^Dwg_Entity_VERTEX_PFACE_FACE;
PBITCODE_H=^BITCODE_H;
TData=PtrInt;
TCounter=Integer;
TProcessLongProcess=procedure(const Data:TData;const Counter:TCounter);
HashDWG_OBJECT_TYPE=class
class function hash(dot:DWG_OBJECT_TYPE; n:longint):SizeUInt;
end;
generic GDWGParser<GUserCtx>=class
type
TDWGObjectLoadProc=procedure(var ZContext:GUserCtx;var DWGContext:TDWGCtx;var DWGObject:Dwg_Object;P:Pointer);
PTDWGObjectData=^TDWGObjectData;
TDWGObjectData=record
LoadEntityProc:TDWGObjectLoadProc;
LoadObjectProc:TDWGObjectLoadProc;
procedure Create;
end;
//work in fpc3.2.2
//TDWGObjectsDataDict=class (specialize TDictionary<DWG_OBJECT_TYPE,TDWGObjectData>)
// function GetMutableValue(key:DWG_OBJECT_TYPE; out PAValue:PTDWGObjectData):boolean;
//end;
TDWGObjectsDataDict=specialize THashmap<DWG_OBJECT_TYPE,TDWGObjectData,HashDWG_OBJECT_TYPE>;
var
DWGObj2LPDict:TDWGObjectsDataDict;
constructor create;
destructor destroy;override;
procedure RegisterDWGEntityLoadProc(const DOT:DWG_OBJECT_TYPE;const LP:TDWGObjectLoadProc);
procedure RegisterDWGObjectLoadProc(const DOT:DWG_OBJECT_TYPE;const LP:TDWGObjectLoadProc);
procedure parseDwg_Data(var ZContext:GUserCtx;var dwg:Dwg_Data;const lpp:TProcessLongProcess;const data:TData);
end;
var
dwg_read_file : function(const filename:pchar;
dwg:PDwg_Data):integer;extdecl;
dxf_read_file : function(const filename:pchar;
dwg:PDwg_Data):integer;extdecl;
dwg_free : procedure(dwg:PDwg_Data);extdecl;
procedure FreeLibreDWG;
procedure LoadLibreDWG(lib : pchar = LibreDWG_Lib; reloadlib : Boolean = False);
// R3: BITCODE_T2Text remains here as a TDWGCtx-aware shim that delegates
// to uzedwgtext.DWGSafeDecodeText. The real handle/text helpers live in
// uzedwghandle and uzedwgtext (both re-exported by this unit).
procedure BITCODE_T2Text(const p:BITCODE_T;constref DWGContext:TDWGCtx;out text:string);
function DWG_V2Str(v:DWG_VERSION_TYPE):string;
function DWGReadCodeIsCritical(Code:integer):Boolean;
function DWGReadCodeToText(Code:integer):string;
// Pure copy of a LIBREDWG line geometry into a ZCAD-shaped record.
// Lives in dwgproc so tests can verify the Z-coord fix without ZCAD deps.
procedure DWGCopyLineEndpoints(const Line:Dwg_Entity_LINE;out Endpoints:TDWGLineEndpoints);
function DWGLineEndpointsAreZeroLength(const Endpoints:TDWGLineEndpoints):Boolean;
// Stage 5 (TZ §12.5): pure scalar copies into the mirror records above.
// Each routine is pointer-aware (nil source produces a zeroed record) so
// callers can drive them straight from LibreDWG output without first
// checking for missing payloads. The text decode goes through
// DWGSafeDecodeText so the loader does not crash on a stripped-down
// fixture that omits the payload field.
procedure DWGCopyCircleProps(const Circle:Dwg_Entity_CIRCLE;
out Props:TDWGCircleProps);
procedure DWGCopyArcProps(const Arc:Dwg_Entity_ARC;out Props:TDWGArcProps);
procedure DWGCopyPointProps(const Point:Dwg_Entity_POINT;
out Props:TDWGPointProps);
procedure DWGCopyTextProps(const Text:Dwg_Entity_TEXT;
Version:DWG_VERSION_TYPE;out Props:TDWGTextProps); overload;
procedure DWGCopyTextProps(const Text:Dwg_Entity_TEXT;
Version:DWG_VERSION_TYPE;Codepage:Integer;out Props:TDWGTextProps); overload;
function DWGTextUsesAlignmentPoint(DataFlags,HorizAlignment,
VertAlignment:Integer):Boolean;
procedure DWGTextEffectiveInsertPoint(const Props:TDWGTextProps;
out X,Y,Z:Double);
function DWGTextAlignmentToJustifyKind(HorizAlignment,
VertAlignment:Integer;
DefaultJustify:TDWGTextJustifyKind=dwtjTopLeft):TDWGTextJustifyKind;
procedure DWGCopyMTextProps(const MText:Dwg_Entity_MTEXT;
Version:DWG_VERSION_TYPE;out Props:TDWGMTextProps); overload;
procedure DWGCopyMTextProps(const MText:Dwg_Entity_MTEXT;
Version:DWG_VERSION_TYPE;Codepage:Integer;out Props:TDWGMTextProps); overload;
procedure DWGCopyInsertProps(const Insert:Dwg_Entity_INSERT;
out Props:TDWGInsertProps); overload;
procedure DWGCopyInsertProps(const Insert:Dwg_Entity_MINSERT;
out Props:TDWGInsertProps); overload;
function DWGMTextAttachmentToJustify(Attachment:Integer;
DefaultJustify:TDWGMTextJustify=dwgmtjTopLeft):TDWGMTextJustify;
function DWGLWPolylineWidthRecordCount(const Props:TDWGLWPolylineProps):Integer;
procedure DWGCopyLWPolylineProps(const LWP:Dwg_Entity_LWPOLYLINE;
out Props:TDWGLWPolylineProps);
function DWGProxyEntityPayloadLooksSane(PProxy:PDwg_Entity_PROXY_ENTITY):Boolean;
procedure DWGCopyProxyEntityPayload(PProxy:PDwg_Entity_PROXY_ENTITY;
out Payload:TDWGProxyEntityPayload);
function DWGCopyProxyEntityPayloadOrPreview(PProxy:PDwg_Entity_PROXY_ENTITY;
const DWGObject:Dwg_Object;out Payload:TDWGProxyEntityPayload;
out UsedPreview:Boolean):Boolean;
// LibreDWG exposes custom/zombie entity proxy graphics on the common
// entity preview fields even when fixedtype is DWG_TYPE_UNKNOWN_ENT.
// Some files leave preview_is_proxy unset, so the implementation also
// accepts preview data whose header matches the proxy graphic stream.
function DWGCopyEntityPreviewProxyPayload(const DWGObject:Dwg_Object;
out Payload:TDWGProxyEntityPayload):Boolean;
procedure DWGCopy3DFaceProps(const Face:Dwg_Entity__3DFACE;
out Props:TDWG3DFaceProps);
procedure DWGCopySolidProps(const Solid:Dwg_Entity_SOLID;
out Props:TDWGSolidProps);
procedure DWGCopyEllipseProps(const Ellipse:Dwg_Entity_ELLIPSE;
out Props:TDWGEllipseProps);
procedure DWGCopySplineProps(const Spline:Dwg_Entity_SPLINE;
out Props:TDWGSplineProps);
procedure DWGCopyHatchProps(const Hatch:Dwg_Entity_HATCH;
Version:DWG_VERSION_TYPE;out Props:TDWGHatchProps); overload;
procedure DWGCopyHatchProps(const Hatch:Dwg_Entity_HATCH;
Version:DWG_VERSION_TYPE;Codepage:Integer;out Props:TDWGHatchProps); overload;
function DWGHatchArcSampleAngle(StartAngle,EndAngle:Double;
IsCounterClockwise:Boolean;SampleIndex,SampleCount:Integer):Double;
procedure DWGCopyPolyline2DRefProps(const Polyline:Dwg_Entity_POLYLINE_2D;
out Props:TDWGPolylineRefProps);
procedure DWGCopyPolyline3DRefProps(const Polyline:Dwg_Entity_POLYLINE_3D;
out Props:TDWGPolylineRefProps);
procedure DWGCopyPolylineMeshRefProps(
const Polyline:Dwg_Entity_POLYLINE_MESH;
out Props:TDWGPolylineRefProps);
procedure DWGCopyPolylinePFaceRefProps(
const Polyline:Dwg_Entity_POLYLINE_PFACE;
out Props:TDWGPolylineRefProps);
implementation
type
PDwg_Object_Entity=^Dwg_Object_Entity;
var
hlib : tlibhandle;
class function HashDWG_OBJECT_TYPE.hash(dot:DWG_OBJECT_TYPE; n:longint):SizeUInt;
begin
result:=ord(dot) mod SizeUInt(n);
end;
procedure TDWGCtx.CreateRec(var ADWG:Dwg_Data);
begin
DWG:=ADWG;
DWGVer:=ADWG.HEADER.version;
if DWGVer=R_INVALID then
DWGVer:=ADWG.HEADER.from_version;
DWGCodePage:=ADWG.HEADER.codepage;
end;
procedure GDWGParser.TDWGObjectData.Create;
begin
LoadEntityProc:=nil;
LoadObjectProc:=nil;
end;
procedure GDWGParser.RegisterDWGEntityLoadProc(const DOT:DWG_OBJECT_TYPE;const LP:TDWGObjectLoadProc);
var
dod:TDWGObjectData;
begin
// Stage 1: catch double registration so a later mapper does not silently
// overwrite an earlier one. The hashmap container does not provide a public
// contains() variant on the FPC version we target, so we look up via
// GetValue and fall back to insert when the key is absent.
if DWGObj2LPDict.GetValue(DOT,dod) then
raise Exception.Create(format(rsHandlerAlreadyReg,[Ord(DOT)]));
dod.Create;
dod.LoadEntityProc:=LP;
dod.LoadObjectProc:=nil;
DWGObj2LPDict.insert(DOT,dod);
end;
procedure GDWGParser.RegisterDWGObjectLoadProc(const DOT:DWG_OBJECT_TYPE;const LP:TDWGObjectLoadProc);
var
dod:TDWGObjectData;
begin
if DWGObj2LPDict.GetValue(DOT,dod) then
raise Exception.Create(format(rsHandlerAlreadyReg,[Ord(DOT)]));
dod.Create;
dod.LoadEntityProc:=nil;
dod.LoadObjectProc:=LP;
DWGObj2LPDict.insert(DOT,dod);
end;
procedure GDWGParser.parseDwg_Data(var ZContext:GUserCtx;var dwg:Dwg_Data;const lpp:TProcessLongProcess;const data:TData);
//work in fpc3.2.2
//var
// i:BITCODE_BL;
// pdod:PTDWGObjectData;
// DWGContext:TDWGCtx;
//begin
// DWGContext.CreateRec(dwg);
// if DWGObj2LPDict<>nil then begin
// i:=0;
// while (i<dwg.num_objects) do begin
// if DWGObj2LPDict.GetMutableValue(dwg.&object[i].fixedtype,pdod) then begin
// if pdod^.LoadEntityProc<>nil then
// pdod^.LoadEntityProc(ZContext,DWGContext,dwg.&object[i],dwg.&object[i].tio.entity^.tio.UNUSED)
// else if pdod^.LoadObjectProc<>nil then
// pdod^.LoadObjectProc(ZContext,DWGContext,dwg.&object[i],dwg.&object[i].tio.&object^.tio.DUMMY);
// end;
// if @lpp<>nil then
// lpp(data,i);
// inc(i);
// end;
// end;
//end;
// Issue #1213: when no mapper is registered for an ENTITY fixedtype,
// fall back to the UNKNOWN_ENT handler so primitives ZCAD does not model
// directly (e.g. MULTILEADER, MLINE, IMAGE, TABLE) still reach a handler
// and surface as proxy entities via their preview-graphics bytes.
// fpdwginspect's TDWGObjectFactory.CreateMaterializedObject performs the
// analogous fallback to its TDWGUnknownMapper — which is precisely why
// `fpdwginspect --dump-unknown` lists primitives the ZCAD loader was
// silently dropping.
//
// Scope notes:
// * Only ENTITY supertype gets the fallback. OBJECT supertype types
// already silently skipped are metadata, not drawable primitives;
// the fixedtype histogram diagnostic still enumerates them.
// * VERTEX_* and SEQEND-like child entities are consumed inside their
// parent polyline / insert mapper. Routing them through UNKNOWN_ENT
// would emit a spurious "unknown entity" warning for every vertex,
// so they are excluded explicitly.
// * UNUSED / FREED slots carry no payload to dispatch on.
function IsParentOwnedChildEntity(FixedType:DWG_OBJECT_TYPE):Boolean;
begin
case FixedType of
DWG_TYPE_VERTEX_2D,
DWG_TYPE_VERTEX_3D,
DWG_TYPE_VERTEX_MESH,
DWG_TYPE_VERTEX_PFACE,
DWG_TYPE_VERTEX_PFACE_FACE:
Result:=True;
else
Result:=False;
end;
end;
function ResolveFallbackDispatch(const Obj:Dwg_Object;
out FallbackDod:TDWGObjectData):Boolean;
begin
Result:=False;
if Obj.supertype<>DWG_SUPERTYPE_ENTITY then
Exit;
if (Obj.fixedtype=DWG_TYPE_UNUSED) or (Obj.fixedtype=DWG_TYPE_FREED) then
Exit;
if IsParentOwnedChildEntity(Obj.fixedtype) then
Exit;
Result:=DWGObj2LPDict.GetValue(DWG_TYPE_UNKNOWN_ENT,FallbackDod);
end;
var
i:BITCODE_BL;
dod:TDWGObjectData;
DWGContext:TDWGCtx;
Dispatched:Boolean;
begin
DWGContext.CreateRec(dwg);
DWGNormalizeObjectHandles(dwg);
if DWGObj2LPDict<>nil then begin
i:=0;
while (i<dwg.num_objects) do begin
Dispatched:=DWGObj2LPDict.GetValue(dwg.&object[i].fixedtype,dod);
if not Dispatched then
Dispatched:=ResolveFallbackDispatch(dwg.&object[i],dod);
if Dispatched then begin
if dod.LoadEntityProc<>nil then begin
if dwg.&object[i].tio.entity<>nil then
dod.LoadEntityProc(ZContext,DWGContext,dwg.&object[i],
dwg.&object[i].tio.entity^.tio.UNUSED)
else
dod.LoadEntityProc(ZContext,DWGContext,dwg.&object[i],nil);
end else if dod.LoadObjectProc<>nil then begin
if dwg.&object[i].tio.&object<>nil then
dod.LoadObjectProc(ZContext,DWGContext,dwg.&object[i],
dwg.&object[i].tio.&object^.tio.DUMMY)
else
dod.LoadObjectProc(ZContext,DWGContext,dwg.&object[i],nil);
end;
end;
// Assigned(lpp), not @lpp<>nil: for a procedural variable @lpp yields
// the address of the variable itself (always non-nil), so the old guard
// always fired and called through a nil lpp. Callers that pass nil (no
// progress callback) — e.g. the unit tests — would crash. Assigned()
// tests the procedure value, so a nil callback is now correctly skipped.
if Assigned(lpp) then
lpp(data,i);
inc(i);
end;
end;
end;
function DWGReadCodeIsCritical(Code:integer):Boolean;
const
CriticalMask =
Ord(DWG_ERR_CLASSESNOTFOUND) or
Ord(DWG_ERR_SECTIONNOTFOUND) or
Ord(DWG_ERR_PAGENOTFOUND) or
Ord(DWG_ERR_INTERNALERROR) or
Ord(DWG_ERR_INVALIDDWG) or
Ord(DWG_ERR_IOERROR) or
Ord(DWG_ERR_OUTOFMEM);
begin
Result := (Code and CriticalMask) <> 0;
end;
function DWGReadCodeToText(Code:integer):string;
procedure AddFlag(Mask:DWG_ERROR;const Name:string);
begin
if (Code and Ord(Mask)) = 0 then
Exit;
if Result <> '' then
Result := Result + ',';
Result := Result + Name;
end;
begin
Result := '';
if Code = Ord(DWG_NOERR) then
Exit('DWG_NOERR');
AddFlag(DWG_ERR_WRONGCRC, 'DWG_ERR_WRONGCRC');
AddFlag(DWG_ERR_NOTYETSUPPORTED, 'DWG_ERR_NOTYETSUPPORTED');
AddFlag(DWG_ERR_UNHANDLEDCLASS, 'DWG_ERR_UNHANDLEDCLASS');
AddFlag(DWG_ERR_INVALIDTYPE, 'DWG_ERR_INVALIDTYPE');
AddFlag(DWG_ERR_INVALIDHANDLE, 'DWG_ERR_INVALIDHANDLE');
AddFlag(DWG_ERR_INVALIDEED, 'DWG_ERR_INVALIDEED');
AddFlag(DWG_ERR_VALUEOUTOFBOUNDS, 'DWG_ERR_VALUEOUTOFBOUNDS');
AddFlag(DWG_ERR_CLASSESNOTFOUND, 'DWG_ERR_CLASSESNOTFOUND');
AddFlag(DWG_ERR_SECTIONNOTFOUND, 'DWG_ERR_SECTIONNOTFOUND');
AddFlag(DWG_ERR_PAGENOTFOUND, 'DWG_ERR_PAGENOTFOUND');
AddFlag(DWG_ERR_INTERNALERROR, 'DWG_ERR_INTERNALERROR');
AddFlag(DWG_ERR_INVALIDDWG, 'DWG_ERR_INVALIDDWG');
AddFlag(DWG_ERR_IOERROR, 'DWG_ERR_IOERROR');
AddFlag(DWG_ERR_OUTOFMEM, 'DWG_ERR_OUTOFMEM');
if Result = '' then
Result := 'DWG_ERR_UNKNOWN';
end;
//work in fpc3.2.2
//function GDWGParser.TDWGObjectsDataDict.GetMutableValue(key:DWG_OBJECT_TYPE; out PAValue:PTDWGObjectData):Boolean;
//var
// LIndex: SizeInt;
// LHash: UInt32;
//begin
// LIndex := FindBucketIndex(FItems, key, LHash);
//
// if LIndex < 0 then begin
// result:=false;
// PAValue:=nil;
// end else begin
// result:=true;
// PAValue:=@FItems[LIndex].Pair.Value;
// end;
//end;
constructor GDWGParser.create;
begin
DWGObj2LPDict:=TDWGObjectsDataDict.create;
end;
destructor GDWGParser.destroy;
begin
DWGObj2LPDict.Free;
end;
function DWG_V2Str(v:DWG_VERSION_TYPE):string;
begin
if Ord(v)>Ord(R_AFTER)then
v:=R_AFTER;
result:=GetEnumName(typeinfo(v),Ord(v));
end;
procedure BITCODE_T2Text(const p:BITCODE_T;constref DWGContext:TDWGCtx;out text:string);
begin
// R3: defer to the version-based helper in uzedwgtext so the actual
// decoding rules live in one place. We pass the parser's resolved
// header version/codepage (the same ones TDWGCtx.CreateRec populates).
DWGSafeDecodeText(p,DWGContext.DWGVer,DWGContext.DWGCodePage,text);
end;
procedure DWGCopyLineEndpoints(const Line:Dwg_Entity_LINE;out Endpoints:TDWGLineEndpoints);
begin
Endpoints.StartX:=Line.start.x;
Endpoints.StartY:=Line.start.y;
Endpoints.StartZ:=Line.start.z;
Endpoints.EndX:=Line.end_.x;
Endpoints.EndY:=Line.end_.y;
Endpoints.EndZ:=Line.end_.z;
end;
function DWGLineEndpointsAreZeroLength(const Endpoints:TDWGLineEndpoints):Boolean;
begin
Result:=(Endpoints.StartX=Endpoints.EndX) and
(Endpoints.StartY=Endpoints.EndY) and
(Endpoints.StartZ=Endpoints.EndZ);
end;
procedure DWGCopyCircleProps(const Circle:Dwg_Entity_CIRCLE;
out Props:TDWGCircleProps);
begin
Props.CenterX:=Circle.center.x;
Props.CenterY:=Circle.center.y;
Props.CenterZ:=Circle.center.z;
Props.Radius:=Circle.radius;
Props.Thickness:=Circle.thickness;
end;
procedure DWGCopyArcProps(const Arc:Dwg_Entity_ARC;out Props:TDWGArcProps);
begin
Props.CenterX:=Arc.center.x;
Props.CenterY:=Arc.center.y;
Props.CenterZ:=Arc.center.z;
Props.Radius:=Arc.radius;
Props.Thickness:=Arc.thickness;
Props.StartAngle:=Arc.start_angle;
Props.EndAngle:=Arc.end_angle;
end;
procedure DWGCopyPointProps(const Point:Dwg_Entity_POINT;
out Props:TDWGPointProps);
begin
Props.X:=Point.x;
Props.Y:=Point.y;
Props.Z:=Point.z;
Props.Thickness:=Point.thickness;
Props.XAngle:=Point.x_ang;
end;
procedure DWGCopyTextProps(const Text:Dwg_Entity_TEXT;
Version:DWG_VERSION_TYPE;out Props:TDWGTextProps);
begin
DWGCopyTextProps(Text,Version,-1,Props);
end;
procedure DWGCopyTextProps(const Text:Dwg_Entity_TEXT;
Version:DWG_VERSION_TYPE;Codepage:Integer;out Props:TDWGTextProps);
begin
Props.DataFlags:=Text.dataflags;
Props.InsertX:=Text.ins_pt.x;
Props.InsertY:=Text.ins_pt.y;
Props.InsertZ:=Text.elevation;
Props.AlignX:=Text.alignment_pt.x;
Props.AlignY:=Text.alignment_pt.y;
Props.Height:=Text.height;
Props.Rotation:=Text.rotation;
Props.Oblique:=Text.oblique_angle;
Props.WidthFactor:=Text.width_factor;
Props.Generation:=Text.generation;
Props.HorizAlignment:=Text.horiz_alignment;
Props.VertAlignment:=Text.vert_alignment;
DWGSafeDecodeText(Text.text_value,Version,Codepage,Props.Value);
end;
function DWGTextUsesAlignmentPoint(DataFlags,HorizAlignment,
VertAlignment:Integer):Boolean;
begin
// Some DWG files carry dataflags bit 2 on default Left text while
// alignment_pt remains 0,0. The actual 72/73 alignment codes decide
// whether the second point is meaningful.
Result:=(HorizAlignment<>0) or (VertAlignment<>0);
end;
procedure DWGTextEffectiveInsertPoint(const Props:TDWGTextProps;
out X,Y,Z:Double);
begin
if DWGTextUsesAlignmentPoint(Props.DataFlags,Props.HorizAlignment,
Props.VertAlignment) then begin
X:=Props.AlignX;
Y:=Props.AlignY;
end else begin
X:=Props.InsertX;
Y:=Props.InsertY;
end;
Z:=Props.InsertZ;
end;
function DWGTextAlignmentToJustifyKind(HorizAlignment,
VertAlignment:Integer;
DefaultJustify:TDWGTextJustifyKind=dwtjTopLeft):TDWGTextJustifyKind;
const
DWGTextMaxVertAlignment=3;
DWGTextMaxHorizAlignment=5;
DWGTextJustifyMap:array[0..3,0..5] of TDWGTextJustifyKind=(
(dwtjLeft,dwtjCenter,dwtjRight,dwtjLeft,dwtjMiddleCenter,dwtjLeft),
(dwtjBottomLeft,dwtjBottomCenter,dwtjBottomRight,dwtjBottomLeft,
dwtjMiddleCenter,dwtjBottomLeft),
(dwtjMiddleLeft,dwtjMiddleCenter,dwtjMiddleRight,dwtjMiddleLeft,
dwtjMiddleCenter,dwtjMiddleLeft),
(dwtjTopLeft,dwtjTopCenter,dwtjTopRight,dwtjTopLeft,
dwtjMiddleCenter,dwtjTopLeft)
);
begin
if (VertAlignment<0) or (VertAlignment>DWGTextMaxVertAlignment) then
Exit(DefaultJustify);
if (HorizAlignment<0) or (HorizAlignment>DWGTextMaxHorizAlignment) then
Exit(DefaultJustify);
Result:=DWGTextJustifyMap[VertAlignment,HorizAlignment];
end;
procedure DWGCopyMTextProps(const MText:Dwg_Entity_MTEXT;
Version:DWG_VERSION_TYPE;out Props:TDWGMTextProps);
begin
DWGCopyMTextProps(MText,Version,-1,Props);
end;
procedure DWGCopyMTextProps(const MText:Dwg_Entity_MTEXT;
Version:DWG_VERSION_TYPE;Codepage:Integer;out Props:TDWGMTextProps);
begin
Props.InsertX:=MText.ins_pt.x;
Props.InsertY:=MText.ins_pt.y;
Props.InsertZ:=MText.ins_pt.z;
Props.XAxisX:=MText.x_axis_dir.x;
Props.XAxisY:=MText.x_axis_dir.y;
Props.XAxisZ:=MText.x_axis_dir.z;
Props.Rotation:=ArcTan2(Props.XAxisY,Props.XAxisX);
Props.RectWidth:=MText.rect_width;
Props.RectHeight:=MText.rect_height;
Props.TextHeight:=MText.text_height;
Props.Attachment:=MText.attachment;
Props.LineSpaceFactor:=MText.linespace_factor;
DWGSafeDecodeText(MText.text,Version,Codepage,Props.Value);
end;
function DWGMTextAttachmentToJustify(Attachment:Integer;
DefaultJustify:TDWGMTextJustify=dwgmtjTopLeft):TDWGMTextJustify;
begin
case Attachment of
1: Result:=dwgmtjTopLeft;
2: Result:=dwgmtjTopCenter;
3: Result:=dwgmtjTopRight;
4: Result:=dwgmtjMiddleLeft;
5: Result:=dwgmtjMiddleCenter;
6: Result:=dwgmtjMiddleRight;
7: Result:=dwgmtjBottomLeft;
8: Result:=dwgmtjBottomCenter;
9: Result:=dwgmtjBottomRight;
else
Result:=DefaultJustify;
end;
end;
function DWGLWPolylineWidthRecordCount(const Props:TDWGLWPolylineProps):Integer;
begin
// ZCAD GDBObjLWPolyline stores one width record per vertex. Open polylines
// ignore the trailing generated segment later, but CalcWidthSegment still
// reads the final width slot while building its cached geometry.
Result:=Length(Props.Vertices);
end;
procedure DWGCopyLWPolylineProps(const LWP:Dwg_Entity_LWPOLYLINE;
out Props:TDWGLWPolylineProps);
type
PBitcode2RD=^BITCODE_2RD;
PBitcodeBD=^BITCODE_BD;
PLWPWidth=^Dwg_LWPOLYLINE_width;
var
i,n:Integer;
pPoint:PBitcode2RD;
pBulge:PBitcodeBD;
pWidth:PLWPWidth;
begin
// Stage 5: bit 512 of `flag` marks a closed polyline (per LibreDWG header
// comments). Bulge / width arrays are only consulted when their counters
// match num_points; mismatched arrays are treated as missing so a
// stripped fixture cannot dereference into garbage.
Props.Closed:=(LWP.flag and 512)<>0;
Props.ConstWidth:=LWP.const_width;
Props.Elevation:=LWP.elevation;
Props.Thickness:=LWP.thickness;
n:=LWP.num_points;
if n<0 then
n:=0;
SetLength(Props.Vertices,n);
if (n>0) and (LWP.points<>nil) then begin
pPoint:=PBitcode2RD(LWP.points);
for i:=0 to n-1 do begin
Props.Vertices[i].X:=pPoint^.x;
Props.Vertices[i].Y:=pPoint^.y;
Props.Vertices[i].StartWidth:=LWP.const_width;
Props.Vertices[i].EndWidth:=LWP.const_width;
Props.Vertices[i].Bulge:=0;
Inc(pPoint);
end;
end;
if (n>0) and (LWP.num_bulges=BITCODE_BL(n)) and (LWP.bulges<>nil) then begin
pBulge:=PBitcodeBD(LWP.bulges);
for i:=0 to n-1 do begin
Props.Vertices[i].Bulge:=pBulge^;
Inc(pBulge);
end;
end;
if (n>0) and (LWP.num_widths=BITCODE_BL(n)) and (LWP.widths<>nil) then begin
pWidth:=PLWPWidth(LWP.widths);
for i:=0 to n-1 do begin
Props.Vertices[i].StartWidth:=pWidth^.start;
Props.Vertices[i].EndWidth:=pWidth^.end_;
Inc(pWidth);
end;
end;
end;
function DWGProxyEntityPayloadLooksSane(PProxy:PDwg_Entity_PROXY_ENTITY):Boolean;
const
MAX_PROXY_GRAPHIC_BYTES=128*1024*1024;
begin
Result:=False;
if PProxy=nil then
Exit;
if PProxy^.proxy_id<>BITCODE_BL(Ord(DWG_TYPE_PROXY_ENTITY)) then
Exit;
if PProxy^.from_dxf>1 then
Exit;
if PProxy^.proxy_data_size>BITCODE_BL(MAX_PROXY_GRAPHIC_BYTES) then
Exit;
if (PProxy^.proxy_data_size>0) and (PProxy^.proxy_data=nil) then
Exit;
Result:=True;
end;
procedure DWGCopyProxyEntityPayload(PProxy:PDwg_Entity_PROXY_ENTITY;
out Payload:TDWGProxyEntityPayload);
function BLToInt(Value:BITCODE_BL):Integer;
begin
if Value>BITCODE_BL(High(Integer)) then
Result:=High(Integer)
else
Result:=Integer(Value);
end;
var
ByteCount:Integer;
begin
Payload.ProxyID:=0;
Payload.ClassID:=0;
Payload.DWGVersions:=0;
Payload.MaintVersion:=0;
Payload.DWGVersion:=0;
Payload.FromDXF:=0;
Payload.EntityDataSize:=0;
Payload.HasGraphic:=False;
SetLength(Payload.Graphic,0);
if PProxy=nil then
Exit;
Payload.ProxyID:=BLToInt(PProxy^.proxy_id);
Payload.ClassID:=BLToInt(PProxy^.class_id);
Payload.DWGVersions:=BLToInt(PProxy^.dwg_versions);
Payload.MaintVersion:=BLToInt(PProxy^.maint_version);
Payload.DWGVersion:=BLToInt(PProxy^.dwg_version);
Payload.FromDXF:=PProxy^.from_dxf;
Payload.EntityDataSize:=BLToInt(PProxy^.data_size);
if not DWGProxyEntityPayloadLooksSane(PProxy) then
Exit;
if (PProxy^.proxy_data=nil) or (PProxy^.proxy_data_size=0) then
Exit;
if PProxy^.proxy_data_size>BITCODE_BL(High(Integer)) then
Exit;
ByteCount:=Integer(PProxy^.proxy_data_size);
SetLength(Payload.Graphic,ByteCount);
Move(PProxy^.proxy_data^,Payload.Graphic[0],ByteCount);
Payload.HasGraphic:=ByteCount>0;
end;
function DWGCopyProxyEntityPayloadOrPreview(PProxy:PDwg_Entity_PROXY_ENTITY;
const DWGObject:Dwg_Object;out Payload:TDWGProxyEntityPayload;
out UsedPreview:Boolean):Boolean;
var
ExplicitPayload,PreviewPayload:TDWGProxyEntityPayload;
begin
UsedPreview:=False;
DWGCopyProxyEntityPayload(PProxy,ExplicitPayload);
Payload:=ExplicitPayload;
if ExplicitPayload.HasGraphic then begin
Result:=True;
Exit;
end;
if DWGCopyEntityPreviewProxyPayload(DWGObject,PreviewPayload) then begin
Payload:=PreviewPayload;
UsedPreview:=True;
Result:=True;
Exit;
end;
Result:=False;
end;
function DWGCopyEntityPreviewProxyPayload(const DWGObject:Dwg_Object;
out Payload:TDWGProxyEntityPayload):Boolean;
const
MAX_PROXY_PREVIEW_COMMAND_COUNT=100000;
function BLToInt(Value:BITCODE_BL):Integer;
begin
if Value>BITCODE_BL(High(Integer)) then
Result:=High(Integer)
else
Result:=Integer(Value);
end;
function BLLToInt(Value:BITCODE_BLL):Integer;
begin
if Value>BITCODE_BLL(High(Integer)) then
Result:=High(Integer)
else
Result:=Integer(Value);
end;
function PreviewLooksLikeProxyGraphic(Ent:PDwg_Object_Entity;
ByteCount:Integer):Boolean;
var
Header:array[0..7] of Byte;
ChunkSize,CommandCount:Cardinal;
begin
Result:=False;
if (Ent=nil) or (Ent^.preview=nil) or (ByteCount<SizeOf(Header)) then
Exit;
Move(Ent^.preview^,Header[0],SizeOf(Header));
Move(Header[0],ChunkSize,SizeOf(ChunkSize));
Move(Header[4],CommandCount,SizeOf(CommandCount));
Result:=(ChunkSize=Cardinal(ByteCount))
and (CommandCount>0)
and (CommandCount<Cardinal(MAX_PROXY_PREVIEW_COMMAND_COUNT));
end;
var
Ent:PDwg_Object_Entity;