-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYJK.cs
2736 lines (2436 loc) · 128 KB
/
YJK.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[assembly: ExtensionApplication(typeof(CAD_YJK.YJK))]
/// <summary>
/// 主命名空间,所有类均在此定义
/// </summary>
namespace CAD_YJK
{
/// <summary>
/// 主程序类,CAD所有接口在此处定义
/// </summary>
public class YJK : IExtensionApplication
{
List<double[]> beamRange = null; // 梁范围,每个元素是一个数组,每个数组由两个数组成
List<double[]> beamLineRange = null; // 梁内钢筋范围,每个元素是一个数组,每个数组由两个数组成
List<double[]> profileRange = null; // 截面Y范围,每个元素是一个数组,每个数组由两个数组成
List<List<double[]>> profileSplits = null; // 截面X范围,每个元素代表一个梁的截面(列表),
// 其元素是一个数组,每个数组由两个数组成
List<List<DBText>> profileSectionText = null; // 获取所有截面标注信息
List<List<AlignedDimension>> beamDimensions = null; // 按梁分类钢筋尺寸标注信息
List<List<BoundingPolyline>> beamLineVertical = null; // 梁线(竖线),计算锚固长度的边线
List<BoundingPolyline> steelBarTable = null; // 钢筋表,所有水平钢筋组成的表
Dictionary<int, List<int>> steelBarTableBeam = null; // 梁-钢筋表
Dictionary<int, int> steelBarTableBeamReverse = null; // 反查钢筋梁表
Dictionary<int, Dictionary<int, Dictionary<double, List<BoundingPolyline>>>>
steelBarBeamProfileRows = null; // 梁-截面-行-钢筋表
Dictionary<int, Dictionary<int, Dictionary<string, string>>>
steelBarBeamProfileAnno = null; // 梁-截面-行-标注表
Dictionary<double, List<int>> steelBarTableStart = null; // 起始锚固长度钢筋表
Dictionary<int, double> steelBarTableStartReverse = null; // 反查起始锚固长度表
Dictionary<double, List<int>> steelBarTableEnd = null; // 末尾锚固长度钢筋表
Dictionary<int, double> steelBarTableEndReverse = null; // 反查末尾锚固长度表
/// <summary>
/// 初始化命令,CAD打开时执行,本程序无需进行初始化
/// </summary>
public void Initialize() { }
/// <summary>
/// 析构命令,CAD关闭前执行,本程序无需析构
/// </summary>
public void Terminate() { }
/// <summary>
/// 显示单根钢筋参数
/// </summary>
[CommandMethod("ShowSteel")]
public void SelectOneSteel()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
if (steelBarTable == null) // 若无钢筋表数据,则要求初始化
{
ed.WriteMessage("\n请先运行初始化命令 I/YJK \n");
return;
}
PromptEntityResult entityResult = ed.GetEntity(
new PromptEntityOptions("选择钢筋")
);
Database dwg = ed.Document.Database;
Transaction trans = dwg.TransactionManager.StartTransaction();
try
{
if (entityResult.Status == PromptStatus.OK)
{
Entity e = trans.GetObject(entityResult.ObjectId, OpenMode.ForRead) as Entity;
if (e == null)
throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NullObjectId, "实体未找到");
BoundingPolyline bp = steelBarTable.Find(
(BoundingPolyline b) => b == e
);
if (bp == null)
throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NullObjectId, "实体不在钢筋表中");
ed.WriteMessage(
"\n钢筋数量 {0}, 钢筋直径 {1}",
bp.num, bp.diameter
);
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("problem due to " + ex.Message);
}
finally
{
trans.Dispose();
}
} // public void SelectOneSteel()
/// <summary>
/// 延长所有锚固长度相同的钢筋,并修改尺寸标注
/// </summary>
[CommandMethod("H")]
public void ExtendAllSameSteelBar()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
if (steelBarTable == null ||
steelBarTableStart == null ||
steelBarTableEnd == null ||
steelBarTableStartReverse == null ||
steelBarTableEndReverse == null) // 若无钢筋表数据,则要求初始化
{
ed.WriteMessage("\n请先运行初始化命令 I/YJK \n");
return;
}
PromptEntityResult entityResult = ed.GetEntity(
new PromptEntityOptions("选择钢筋")
);
Database dwg = ed.Document.Database;
Transaction trans = dwg.TransactionManager.StartTransaction();
double startLength = 0; // 起始锚固长度
double endLength = 0; // 末尾锚固长度
List<int> sameStartSteelBar = null; // 相同起始锚固长度钢筋
List<int> sameEndSteelBar = null; // 相同结尾锚固长度钢筋
try
{
if (entityResult.Status == PromptStatus.OK)
{
// 初始化beamDimensions
if (beamDimensions == null)
{
beamDimensions = new List<List<AlignedDimension>>();
for (int i = 0; i < beamRange.Count; i++)
{
beamDimensions.Add(new List<AlignedDimension>());
}
PromptSelectionResult allDimesions = ed.SelectAll(
new SelectionFilter(
new TypedValue[] {
new TypedValue((int)DxfCode.LayerName, Utils.DIM_BEAM_NAME)
}
)
);
// 按梁号分类标注信息
foreach (ObjectId item in allDimesions.Value.GetObjectIds())
{
AlignedDimension ad = trans.GetObject(item, OpenMode.ForRead) as AlignedDimension;
if (ad == null) continue;
int beamIndex = beamRange.FindIndex(
(double[] t) => t[0] <= ad.XLine1Point.Y && ad.XLine1Point.Y <= t[1]
);
if (beamIndex == -1) continue;
beamDimensions[beamIndex].Add(ad);
}
}
Entity e = trans.GetObject(entityResult.ObjectId, OpenMode.ForRead) as Entity;
if (e == null)
throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NullObjectId, "实体未找到");
// 获取选择钢筋的锚固长度以及相同长度的锚固钢筋
int steelIndex = steelBarTable.FindIndex(
(BoundingPolyline b) => b == e
);
startLength = steelBarTableStartReverse[steelIndex];
endLength = steelBarTableEndReverse[steelIndex];
sameStartSteelBar = new List<int>(steelBarTableStart[startLength]);
sameEndSteelBar = new List<int>(steelBarTableEnd[endLength]);
ed.WriteMessage(
"\n起始锚固长度为 " +
startLength.ToString() +
" ,末尾锚固长度为 " +
endLength.ToString() +
"\n有相同起始锚固长度的钢筋 " +
sameStartSteelBar.Count.ToString() +
" 条,有相同末尾锚固长度的钢筋 " +
sameEndSteelBar.Count.ToString() +
" 条\n"
);
} // if (entityResult.Status == PromptStatus.OK)
} // try
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("problem due to " + ex.Message);
}
finally
{
trans.Dispose();
}
// 修改起始锚固长度
trans = dwg.TransactionManager.StartTransaction();
try
{
PromptResult startModifyResult = ed.GetKeywords(
new PromptKeywordOptions("是否修改起始锚固长度 [Y/N] : ", "Y N")
);
if (startModifyResult.Status == PromptStatus.OK &&
"Y".Equals(startModifyResult.StringResult))
{
PromptDoubleResult modifyStartMLength = ed.GetDouble(
new PromptDoubleOptions("请输入修改后的起始锚固长度")
);
foreach (var lineIndex in sameStartSteelBar)
{
steelBarTable[lineIndex].GetLine().Highlight(); // 高亮显示修改线
}
PromptResult isModify = ed.GetKeywords(
new PromptKeywordOptions("高亮显示的线将被修改,是否确认 [Y/N] : ", "Y N")
);
foreach (var lineIndex in sameStartSteelBar)
{
steelBarTable[lineIndex].GetLine().Unhighlight(); // 取消高亮
}
if (isModify.Status == PromptStatus.OK &&
"Y".Equals(isModify.StringResult))
{
foreach (var lineIndex in sameStartSteelBar)
{
double extendLength = modifyStartMLength.Value - startLength;
Entity e = trans.GetObject(
steelBarTable[lineIndex].GetLine().ObjectId,
OpenMode.ForWrite
) as Entity; // 以写方式打开实体
if (e == null) continue;
if (steelBarTable[lineIndex].isPolyline)
{
// 如果实体是多段线,则按如下方式延长
Polyline pl = e as Polyline;
if (pl == null) continue;
// 查找有无尺寸标注端点在钢筋端点处,如有,则与钢筋端点同时移动
Point3d previousPoint = steelBarTable[lineIndex].GetStartPoint();
foreach(AlignedDimension a in beamDimensions[steelBarTableBeamReverse[lineIndex]])
{
if (a.XLine1Point == previousPoint)
{
AlignedDimension aw = trans.GetObject(a.ObjectId, OpenMode.ForWrite) as AlignedDimension;
aw.XLine1Point -= new Vector3d(extendLength, 0, 0);
}
if (a.XLine2Point == previousPoint)
{
AlignedDimension aw = trans.GetObject(a.ObjectId, OpenMode.ForWrite) as AlignedDimension;
aw.XLine2Point -= new Vector3d(extendLength, 0, 0);
}
}
// 向左平移所有索引在指定线段索引前的端点
for (int i = 0; i <= steelBarTable[lineIndex].segmentIndex; i++)
{
Point2d newPoint = pl.GetPoint2dAt(i) - new Vector2d(extendLength, 0);
pl.SetPointAt(i, newPoint);
}
steelBarTable[lineIndex] = new BoundingPolyline(pl);
}
else
{
// 如果实体是直线,则按如下方式延长
Line l = e as Line;
if (l == null) continue;
// 查找有无尺寸标注端点在钢筋端点处,如有,则与钢筋端点同时移动
Point3d previousPoint = steelBarTable[lineIndex].GetStartPoint();
foreach (AlignedDimension a in beamDimensions[steelBarTableBeamReverse[lineIndex]])
{
if (a.XLine1Point == previousPoint)
{
AlignedDimension aw = trans.GetObject(a.ObjectId, OpenMode.ForWrite) as AlignedDimension;
aw.XLine1Point -= new Vector3d(extendLength, 0, 0);
}
if (a.XLine2Point == previousPoint)
{
AlignedDimension aw = trans.GetObject(a.ObjectId, OpenMode.ForWrite) as AlignedDimension;
aw.XLine2Point -= new Vector3d(extendLength, 0, 0);
}
}
l.StartPoint -= new Vector3d(extendLength, 0, 0);
steelBarTable[lineIndex] = new BoundingPolyline(l);
}
// 修改实体关联的钢筋表相关参数
steelBarTableStart[startLength].Remove(lineIndex);
if (steelBarTableStart.ContainsKey(Math.Round(modifyStartMLength.Value, Utils.FIXED_NUM)))
{
steelBarTableStart[Math.Round(modifyStartMLength.Value, Utils.FIXED_NUM)].Add(lineIndex);
}
else
{
steelBarTableStart.Add(
Math.Round(modifyStartMLength.Value, Utils.FIXED_NUM),
new List<int> { lineIndex }
);
}
steelBarTableStartReverse[lineIndex] = Math.Round(modifyStartMLength.Value, Utils.FIXED_NUM);
}
trans.Commit();
ed.WriteMessage("\n起始锚固长度修改完成");
} // if (isModify.Status == PromptStatus.OK && "Y".Equals(isModify.StringResult))
} // if (startModifyResult.Status == PromptStatus.OK && "Y".Equals(startModifyResult.StringResult))
} // try
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("problem due to " + ex.Message);
}
finally
{
trans.Dispose();
}
// 修改末尾锚固长度
trans = dwg.TransactionManager.StartTransaction();
try
{
PromptResult endModifyResult = ed.GetKeywords(
new PromptKeywordOptions("是否修改末尾锚固长度 [Y/N] : ", "Y N")
);
if (endModifyResult.Status == PromptStatus.OK &&
"Y".Equals(endModifyResult.StringResult))
{
PromptDoubleResult modifyEndMLength = ed.GetDouble(
new PromptDoubleOptions("请输入修改后末尾锚固长度")
);
foreach (var lineIndex in sameEndSteelBar)
{
steelBarTable[lineIndex].GetLine().Highlight(); // 高亮显示修改线
}
PromptResult isModify = ed.GetKeywords(
new PromptKeywordOptions("高亮显示的线将被修改,是否确认 [Y/N] : ", "Y N")
);
foreach (var lineIndex in sameEndSteelBar)
{
steelBarTable[lineIndex].GetLine().Unhighlight(); // 取消高亮
}
if (isModify.Status == PromptStatus.OK &&
"Y".Equals(isModify.StringResult))
{
foreach (var lineIndex in sameEndSteelBar)
{
double extendLength = modifyEndMLength.Value - endLength;
Entity e = trans.GetObject(
steelBarTable[lineIndex].GetLine().ObjectId,
OpenMode.ForWrite
) as Entity; // 以写方式打开实体
if (e == null) continue;
if (steelBarTable[lineIndex].isPolyline)
{
// 如果实体是多段线,则按如下方式延长
Polyline pl = e as Polyline;
if (pl == null) continue;
// 查找有无尺寸标注端点在钢筋端点处,如有,则与钢筋端点同时移动
Point3d previousPoint = steelBarTable[lineIndex].GetEndPoint();
foreach (AlignedDimension a in beamDimensions[steelBarTableBeamReverse[lineIndex]])
{
if (a.XLine1Point == previousPoint)
{
AlignedDimension aw = trans.GetObject(a.ObjectId, OpenMode.ForWrite) as AlignedDimension;
aw.XLine1Point += new Vector3d(extendLength, 0, 0);
}
if (a.XLine2Point == previousPoint)
{
AlignedDimension aw = trans.GetObject(a.ObjectId, OpenMode.ForWrite) as AlignedDimension;
aw.XLine2Point += new Vector3d(extendLength, 0, 0);
}
}
// 向右平移所有索引在指定线段索引后的端点
for (int i = steelBarTable[lineIndex].segmentIndex + 1; i < pl.NumberOfVertices; i++)
{
Point2d newPoint = pl.GetPoint2dAt(i) + new Vector2d(extendLength, 0);
pl.SetPointAt(i, newPoint);
}
steelBarTable[lineIndex] = new BoundingPolyline(pl);
}
else
{
// 如果实体是直线,则按如下方式延长
Line l = e as Line;
if (l == null) continue;
// 查找有无尺寸标注端点在钢筋端点处,如有,则与钢筋端点同时移动
Point3d previousPoint = steelBarTable[lineIndex].GetEndPoint();
foreach (AlignedDimension a in beamDimensions[steelBarTableBeamReverse[lineIndex]])
{
if (a.XLine1Point == previousPoint)
{
AlignedDimension aw = trans.GetObject(a.ObjectId, OpenMode.ForWrite) as AlignedDimension;
aw.XLine1Point += new Vector3d(extendLength, 0, 0);
}
if (a.XLine2Point == previousPoint)
{
AlignedDimension aw = trans.GetObject(a.ObjectId, OpenMode.ForWrite) as AlignedDimension;
aw.XLine2Point += new Vector3d(extendLength, 0, 0);
}
}
l.EndPoint += new Vector3d(extendLength, 0, 0);
steelBarTable[lineIndex] = new BoundingPolyline(l);
}
// 修改实体关联的钢筋表相关参数
steelBarTableEnd[endLength].Remove(lineIndex);
if (steelBarTableEnd.ContainsKey(Math.Round(modifyEndMLength.Value, Utils.FIXED_NUM)))
{
steelBarTableEnd[Math.Round(modifyEndMLength.Value, Utils.FIXED_NUM)].Add(lineIndex);
}
else
{
steelBarTableEnd.Add(
Math.Round(modifyEndMLength.Value, Utils.FIXED_NUM),
new List<int> { lineIndex }
);
}
steelBarTableEndReverse[lineIndex] = Math.Round(modifyEndMLength.Value, Utils.FIXED_NUM);
}
trans.Commit();
ed.WriteMessage("\n末尾锚固长度修改完成");
} // if (isModify.Status == PromptStatus.OK && "Y".Equals(isModify.StringResult))
} // if (endModifyResult.Status == PromptStatus.OK && "Y".Equals(endModifyResult.StringResult))
} // try
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("problem due to " + ex.Message);
}
finally
{
trans.Dispose();
}
} // public void ExtendAllSameSteelBar()
/// <summary>
/// 计算dwg文件中所有钢筋锚固长度
/// </summary>
[CommandMethod("I")]
public void CalculateAllSteelBar()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database dwg = ed.Document.Database;
// 计算水平、垂直线,并将结果保存在steelBarTable
// steelBarTableStart, steelBarTableStartReverse
// steelBarTableEnd, steelBarTableEndReverse中
Transaction trans = dwg.TransactionManager.StartTransaction();
try
{
PromptSelectionResult selection = ed.SelectAll();
if (selection.Status == PromptStatus.OK)
{
List<BoundingPolyline> verticalLines = new List<BoundingPolyline>();
List<BoundingPolyline> horizontalLines = new List<BoundingPolyline>();
foreach (ObjectId item in selection.Value.GetObjectIds())
{
Entity e = trans.GetObject(item, OpenMode.ForRead) as Entity;
if (e == null) continue;
Line l = e as Line;
if (l != null)
{
if(Math.Abs(l.Delta.X) <= Utils.DOUBLE_EPS) // 为垂直线
{
verticalLines.Add(new BoundingPolyline(l));
} else if(Math.Abs(l.Delta.Y) <= Utils.DOUBLE_EPS)
{
horizontalLines.Add(new BoundingPolyline(l)); // 为水平线
}
}
else
{
Polyline pl = e as Polyline;
if (pl != null)
{
BoundingPolyline bp = new BoundingPolyline(pl);
Vector3d deltaVector = bp.ls.EndPoint - bp.ls.StartPoint;
if (Math.Abs(deltaVector.X) <= Utils.DOUBLE_EPS) // 为垂直线
{
verticalLines.Add(bp);
}
else if (Math.Abs(deltaVector.Y) <= Utils.DOUBLE_EPS) // 为水平线
{
horizontalLines.Add(bp);
}
} // if (pl != null)
} // else
} // foreach (ObjectId item in selection.Value.GetObjectIds())
// 初始化各类钢筋表
steelBarTable = new List<BoundingPolyline>();
steelBarTableStart = new Dictionary<double, List<int>>();
steelBarTableEnd = new Dictionary<double, List<int>>();
steelBarTableStartReverse = new Dictionary<int, double>();
steelBarTableEndReverse = new Dictionary<int, double>();
foreach (BoundingPolyline l1 in horizontalLines)
{
//计算锚固长度
double startMLength = double.PositiveInfinity;
double endMLength = double.PositiveInfinity;
foreach (BoundingPolyline l2 in verticalLines)
{
Point3dCollection pTemp = new Point3dCollection();
l1.GetLine().IntersectWith(
l2.GetLine(), Intersect.OnBothOperands,
pTemp, IntPtr.Zero, IntPtr.Zero
);
if (pTemp.Count == 1) // 有且只有一个交点
{
startMLength = Math.Min(
pTemp[0].DistanceTo(l1.GetStartPoint()),
startMLength
);
endMLength = Math.Min(
pTemp[0].DistanceTo(l1.GetEndPoint()),
endMLength
);
}
} // foreach (BoundingPolyline l2 in verticalLines)
// 将钢筋添加进钢筋表
int steelBarIndex = steelBarTable.Count;
steelBarTable.Add(l1);
// 添加进起始锚固长度表
if (steelBarTableStart.ContainsKey(Math.Round(startMLength, Utils.FIXED_NUM)))
steelBarTableStart[Math.Round(startMLength, Utils.FIXED_NUM)].Add(steelBarIndex);
else
steelBarTableStart.Add(
Math.Round(startMLength, Utils.FIXED_NUM),
new List<int> { steelBarIndex }
);
// 添加进末尾锚固长度表
if (steelBarTableEnd.ContainsKey(Math.Round(endMLength, Utils.FIXED_NUM)))
steelBarTableEnd[Math.Round(endMLength, Utils.FIXED_NUM)].Add(steelBarIndex);
else
steelBarTableEnd.Add(
Math.Round(endMLength, Utils.FIXED_NUM),
new List<int> { steelBarIndex }
);
// 添加反查表
steelBarTableStartReverse.Add(
steelBarIndex,
Math.Round(startMLength, Utils.FIXED_NUM)
);
steelBarTableEndReverse.Add(
steelBarIndex,
Math.Round(endMLength, Utils.FIXED_NUM)
);
} // foreach (BoundingPolyline l1 in horizontalLines)
ed.WriteMessage("\nInitialize success!");
} // if (selection.Status == PromptStatus.OK)
} // try
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("problem due to " + ex.Message);
}
finally
{
trans.Dispose();
}
} // public void CalculateAllSteelBar()
/// <summary>
/// 处理盈建科软件导出的DWG图纸
/// </summary>
[CommandMethod("YJK")]
public void YJKParse()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database dwg = ed.Document.Database;
// 选择所有纵筋、箍筋将直线改为多段线并调整全局宽度为25
Transaction trans = dwg.TransactionManager.StartTransaction();
try
{
PromptSelectionResult selection = ed.SelectAll(
new SelectionFilter(
new TypedValue[] {
new TypedValue((int)DxfCode.Operator, "<OR"),
new TypedValue((int)DxfCode.LayerName, Utils.BEAM_SECTION_STEEL_NAME),
new TypedValue((int)DxfCode.LayerName, Utils.BEAM_SECTION_GSTEEL_NAME),
new TypedValue((int)DxfCode.Operator, "OR>")
}
)
); // 过滤所有图层名为梁截面纵筋或梁截面箍筋的实体
BlockTableRecord lt = trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
if (lt == null)
throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NullObjectId, "块表记录未找到");
foreach (ObjectId item in selection.Value.GetObjectIds())
{
Entity e = trans.GetObject(item, OpenMode.ForWrite) as Entity;
if (e == null) continue;
// 将直线转化为多段线
Line l = e as Line;
if (l != null)
{
Polyline pl = new Polyline();
pl.Layer = l.Layer;
pl.LineWeight = l.LineWeight;
pl.Linetype = l.Linetype;
pl.Color = l.Color;
pl.AddVertexAt(0, new Point2d(l.StartPoint.ToArray()), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(l.EndPoint.ToArray()), 0, 0, 0);
pl.ConstantWidth = 25;
lt.AppendEntity(pl);
trans.AddNewlyCreatedDBObject(pl, true);
l.Erase();
}
else
{
// 多段线修改全局宽度
Polyline pline = e as Polyline;
if (pline != null)
{
pline.ConstantWidth = 25;
}
}
} // foreach (ObjectId item in selection.Value.GetObjectIds())
trans.Commit();
} // try
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("problem due to " + ex.Message);
}
finally
{
trans.Dispose();
}
// 从DWG图纸中获取梁及钢筋数据
trans = dwg.TransactionManager.StartTransaction();
try
{
// 确定单根梁范围,保存在beamRange中
PromptSelectionResult selection = ed.SelectAll(
new SelectionFilter(
new TypedValue[] {
new TypedValue((int)DxfCode.LayerName, Utils.AXIS_NAME)
}
)
);
beamRange = new List<double[]>();
bool isExisted = false;
foreach (ObjectId item in selection.Value.GetObjectIds())
{
Entity e = trans.GetObject(item, OpenMode.ForRead) as Entity;
if (e == null) continue;
Line l = e as Line; // 轴线均为直线,不是多段线
if (l != null)
{
isExisted = false;
foreach (double[] t in beamRange)
{
if (Math.Abs(l.StartPoint.Y - t[0]) < Utils.DOUBLE_EPS ||
Math.Abs(l.EndPoint.Y - t[0]) < Utils.DOUBLE_EPS) // 轴线一般水平方向对齐,以此进行分组
{
isExisted = true;
}
}
if (!isExisted)
{
beamRange.Add(
new double[] {
l.StartPoint.Y, l.EndPoint.Y
}
);
}
}
} // foreach (ObjectId item in selection.Value.GetObjectIds())
beamRange.Sort(
(double[] t1, double[] t2) => t1[0].CompareTo(t2[0]) // 排序,结果为从下至上(由小到大)
);
// 确定单根梁内部范围,保存在beamLineRange中
// 确定单根梁支座线,保存在beamLineVertical中
beamLineRange = new List<double[]>();
beamLineVertical = new List<List<BoundingPolyline>>();
for (int i = 0; i < beamRange.Count; i++)
{
beamLineRange.Add(new double[] { double.PositiveInfinity, double.NegativeInfinity });
beamLineVertical.Add(new List<BoundingPolyline>());
}
PromptSelectionResult beamSelction = ed.SelectAll(
new SelectionFilter(
new TypedValue[] {
new TypedValue((int)DxfCode.LayerName, Utils.BEAM_SECTION_NAME)
}
)
);
foreach (ObjectId beamLine in beamSelction.Value.GetObjectIds())
{
Line bl = trans.GetObject(beamLine, OpenMode.ForRead) as Line;
if (bl != null)
{
int blIndex = beamRange.FindIndex(
item => item[0] < bl.StartPoint.Y && bl.StartPoint.Y < item[1]
); // 查找梁号,下同
if (blIndex == -1) continue; // 未找到
Vector3d deltaVector = bl.EndPoint - bl.StartPoint;
if (Math.Abs(deltaVector.Y) <= Utils.DOUBLE_EPS) // 为水平线
{
beamLineRange[blIndex][0] = Math.Min(beamLineRange[blIndex][0], bl.StartPoint.Y);
beamLineRange[blIndex][1] = Math.Max(beamLineRange[blIndex][1], bl.EndPoint.Y);
}
}
}
foreach (ObjectId beamLine in beamSelction.Value.GetObjectIds())
{
Line bl = trans.GetObject(beamLine, OpenMode.ForRead) as Line;
if ( bl!= null)
{
int blIndex = beamRange.FindIndex(
item => item[0] < bl.StartPoint.Y && bl.StartPoint.Y < item[1]
);
if (blIndex == -1) continue;
Vector3d deltaVector = bl.EndPoint - bl.StartPoint;
// 为竖直线,且全部包含在梁水平线围合空间中
if ((Math.Abs(deltaVector.X) <= Utils.DOUBLE_EPS)
&& (beamLineRange[blIndex][0] - Utils.DOUBLE_EPS <= bl.StartPoint.Y)
&& (bl.EndPoint.Y <= beamLineRange[blIndex][1] + Utils.DOUBLE_EPS))
{
beamLineVertical[blIndex].Add(new BoundingPolyline(bl));
}
}
}
// 创建钢筋表,将所有钢筋保存在steelBarTable中
// 钢筋分类,保存在steelBarTableBeam及steelBarTableBeamReverse中
steelBarTable = new List<BoundingPolyline>();
steelBarTableBeam = new Dictionary<int, List<int>>();
steelBarTableBeamReverse = new Dictionary<int, int>();
for (int i = 0; i < beamRange.Count; i++)
{
steelBarTableBeam.Add(
i, new List<int>()
);
}
PromptSelectionResult barSelection = ed.SelectAll(
new SelectionFilter(
new TypedValue[] {
new TypedValue((int)DxfCode.LayerName, Utils.BEAM_SECTION_STEEL_NAME)
}
)
);
foreach (ObjectId bar in barSelection.Value.GetObjectIds())
{
Polyline pl = trans.GetObject(bar, OpenMode.ForRead) as Polyline; // 钢筋均为多段线,没有直线
if (pl != null)
{
BoundingPolyline bp = new BoundingPolyline(pl);
int bpIndex = beamRange.FindIndex(
item => item[0] < bp.GetStartPoint().Y && bp.GetStartPoint().Y < item[1]
); // 查找钢筋所在梁号
if (bpIndex == -1) continue;
Vector3d deltaVector = bp.GetEndPoint() - bp.GetStartPoint();
if (Math.Abs(deltaVector.Y) <= Utils.DOUBLE_EPS) // 只考虑水平筋(锚固长度)
{
// 添加进钢筋表、梁-钢筋表、及其反查表
steelBarTableBeam[bpIndex].Add(steelBarTable.Count);
steelBarTableBeamReverse.Add(steelBarTable.Count, bpIndex);
steelBarTable.Add(bp);
// 判断钢筋是否在梁内
bp.isOut = true;
if ((beamLineRange[bpIndex][0] - Utils.DOUBLE_EPS <= bp.GetStartPoint().Y)
&& (bp.GetEndPoint().Y <= beamLineRange[bpIndex][1] + Utils.DOUBLE_EPS))
{
bp.isOut = false; // 如果钢筋位于梁水平线围合空间中,认为钢筋在梁内
}
}
}
} // foreach (ObjectId bar in barSelection.Value.GetObjectIds())
// 计算水平筋锚固长度,并保存在
// steelBarTableStart, steelBarTableStartReverse
// steelBarTableEnd, steelBarTableEndReverse中
steelBarTableStart = new Dictionary<double, List<int>>();
steelBarTableEnd = new Dictionary<double, List<int>>();
steelBarTableStartReverse = new Dictionary<int, double>();
steelBarTableEndReverse = new Dictionary<int, double>();
for (int i = 0; i < beamRange.Count; i++)
{
foreach (int lineIndex in steelBarTableBeam[i])
{
// 计算锚固长度
double startMLength = double.PositiveInfinity;
double endMLength = double.PositiveInfinity;
BoundingPolyline l1 = steelBarTable[lineIndex];
foreach (BoundingPolyline l2 in beamLineVertical[i])
{
Point3dCollection pTemp = new Point3dCollection();
l1.GetLine().IntersectWith(
l2.GetLine(), Intersect.ExtendArgument,
pTemp, IntPtr.Zero, IntPtr.Zero
);
if (pTemp.Count == 1) // 有且仅有一个交点
{
startMLength = Math.Min(
pTemp[0].DistanceTo(l1.GetStartPoint()),
startMLength
);
endMLength = Math.Min(
pTemp[0].DistanceTo(l1.GetEndPoint()),
endMLength
);
}
}
// 添加进起始锚固长度表
if (steelBarTableStart.ContainsKey(Math.Round(startMLength, Utils.FIXED_NUM)))
steelBarTableStart[Math.Round(startMLength, Utils.FIXED_NUM)].Add(lineIndex);
else
steelBarTableStart.Add(
Math.Round(startMLength, Utils.FIXED_NUM),
new List<int> { lineIndex }
);
// 添加进末尾锚固长度表
if (steelBarTableEnd.ContainsKey(Math.Round(endMLength, Utils.FIXED_NUM)))
steelBarTableEnd[Math.Round(endMLength, Utils.FIXED_NUM)].Add(lineIndex);
else
steelBarTableEnd.Add(
Math.Round(endMLength, Utils.FIXED_NUM),
new List<int> { lineIndex }
);
// 添加进对应反查表
steelBarTableStartReverse.Add(
lineIndex,
Math.Round(startMLength, Utils.FIXED_NUM)
);
steelBarTableEndReverse.Add(
lineIndex,
Math.Round(endMLength, Utils.FIXED_NUM)
);
} // foreach (int lineIndex in steelBarTableBeam[i])
} // for (int i = 0; i < beamRange.Count; i++)
// 获取钢筋标注信息(根数、直径)
PromptSelectionResult annoSelection = ed.SelectAll(
new SelectionFilter(
new TypedValue[] {
new TypedValue((int)DxfCode.LayerName, Utils.BEAM_SECTION_ANNO_NAME)
}
)
);
// 按梁号分类标注,提升计算效率
List<List<DBText>> annoInBeam = new List<List<DBText>>();
for (int i = 0; i < beamRange.Count; i++)
{
annoInBeam.Add(new List<DBText>());
}
foreach (ObjectId anno in annoSelection.Value.GetObjectIds())
{
DBText t = trans.GetObject(anno, OpenMode.ForRead) as DBText;
if (t != null)
{
int tIndex = beamRange.FindIndex(
item => item[0] < t.Position.Y && t.Position.Y < item[1]
);
if (tIndex == -1) continue;
annoInBeam[tIndex].Add(t);
}
}
// 分钢筋计算最近标注
for (int i = 0; i < beamRange.Count; i++)
{
foreach (int outLine in steelBarTableBeam[i])
{
if(steelBarTable[outLine].isOut) // 外部钢筋才有相应标注
{
Point3d start = steelBarTable[outLine].GetStartPoint();
Point3d end = steelBarTable[outLine].GetEndPoint();
double distance = double.PositiveInfinity;
DBText ans = null;
foreach (DBText text in annoInBeam[i])
{
Point3d textPoint = text.Position;
if (start.X <= textPoint.X && textPoint.X <= end.X) // 标注水平坐标在钢筋范围内
{
double dis = Math.Abs(textPoint.Y - start.Y);
if (dis < distance)
{
distance = dis;
ans = text; // 标注竖直距离最短
}
}
}
// 如果有钢筋长度极短(e.g. <200mm),可能会找不到标注
// 这种情况也无需考虑钢筋根数问题,该情况只可能出现在悬臂梁上部钢筋中
// 因此本程序选择跳过这种情况
if (ans == null) continue;
// 利用正则表达式匹配钢筋数目和钢筋直径
Match match = Regex.Match(ans.TextString, Utils.ANNO_PATTERN);
if (match != null)
{
steelBarTable[outLine].num = int.Parse(match.Groups[1].Value);
steelBarTable[outLine].diameter = int.Parse(match.Groups[2].Value);
}
} // if(steelBarTable[outLine].isOut)
} // foreach (int outLine in steelBarTableBeam[i])
} // for (int i = 0; i < beamRange.Count; i++)
} // try
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("problem due to " + ex.Message);
}
finally
{
trans.Dispose();
}
// 计算截面处钢筋标注
trans = dwg.TransactionManager.StartTransaction();
try
{
PromptSelectionResult steelBarAll = ed.SelectAll(
new SelectionFilter(
new TypedValue[] {
new TypedValue((int)DxfCode.LayerName, Utils.BEAM_SECTION_STEEL_NAME)
}
)