|
18 | 18 |
|
19 | 19 | import ifcopenshell |
20 | 20 | import ifcopenshell.api.alignment |
21 | | -import ifcopenshell.api.nest |
22 | | -import ifcopenshell.util.stationing |
23 | 21 | from ifcopenshell import entity_instance |
24 | 22 |
|
25 | 23 | from ifcopenshell.api.alignment._get_segment_start_point_label import _get_segment_start_point_label |
26 | 24 |
|
27 | | - |
28 | 25 | def _add_zero_length_segment(file: ifcopenshell.file, layout: entity_instance) -> None: |
29 | 26 | """ |
30 | 27 | Adds a zero length segment to the end of a layout. Also adds a zero length segment to the end of the corresponding geometric curve. |
31 | 28 |
|
32 | | - If the layout already has a zero length segment, nothing is changed |
| 29 | + This function depends on the assumptions made in ifcopenshell.api.alignment.create and is called by that function. |
| 30 | + This is not a general purpose function. |
33 | 31 |
|
34 | 32 | :param layout: An IfcAlignmentHorizontal, IfcAlignmentVertical, or IfcAlignmentCant |
35 | 33 | :return: None |
36 | 34 | """ |
| 35 | + |
37 | 36 | expected_types = ["IfcAlignmentHorizontal", "IfcAlignmentVertical", "IfcAlignmentCant"] |
38 | 37 | if not layout.is_a() in expected_types: |
39 | 38 | raise TypeError( |
40 | 39 | f"Expected layout type to be one of {[_ for _ in expected_types]}, instead received {layout.is_a()}" |
41 | 40 | ) |
42 | | - |
43 | | - if ifcopenshell.api.alignment.has_zero_length_segment(layout): |
44 | | - return |
45 | | - |
46 | | - segment = None |
47 | | - if layout.is_a("IfcAlignmentHorizontal"): |
48 | | - design_parameters = file.createIfcAlignmentHorizontalSegment( |
49 | | - StartPoint=file.createIfcCartesianPoint( |
50 | | - (0.0, 0.0) |
51 | | - ), # this is a little problematic. need to know the end point and tangent |
52 | | - StartDirection=0.0, # of the previous segment, which requires geometry mapping |
53 | | - StartRadiusOfCurvature=0.0, |
54 | | - EndRadiusOfCurvature=0.0, |
55 | | - SegmentLength=0.0, |
56 | | - PredefinedType="LINE", |
57 | | - ) |
58 | | - segment = file.createIfcAlignmentSegment(GlobalId=ifcopenshell.guid.new(), DesignParameters=design_parameters) |
59 | | - elif layout.is_a("IfcAlignmentVertical"): |
60 | | - last_segment_dist_along = 0.0 |
61 | | - last_segment_end_gradient = 0.0 |
62 | | - for rel in layout.IsNestedBy: |
63 | | - if 0 < len(rel.RelatedObjects): |
64 | | - last_segment = rel.RelatedObjects[1] |
65 | | - last_segment_dist_along = ( |
66 | | - last_segment.DesignParameters.StartDistAlong + last_segment.DesignParameters.HorizontalLength |
67 | | - ) |
68 | | - last_segment_end_gradient = last_segment.DesignParameters.EndGradient |
69 | | - |
70 | | - design_parameters = file.createIfcAlignmentVerticalSegment( |
71 | | - StartDistAlong=last_segment_dist_along, |
72 | | - HorizontalLength=0.0, |
73 | | - StartHeight=0.0, |
74 | | - StartGradient=last_segment_end_gradient, |
75 | | - EndGradient=last_segment_end_gradient, |
76 | | - PredefinedType="CONSTANTGRADIENT", |
77 | | - ) |
78 | | - segment = file.createIfcAlignmentSegment(GlobalId=ifcopenshell.guid.new(), DesignParameters=design_parameters) |
79 | | - elif layout.is_a("IfcAlignmentCant"): |
80 | | - last_segment_dist_along = 0.0 |
81 | | - last_segment_cant_left = 0.0 |
82 | | - last_segment_cant_right = 0.0 |
83 | | - for rel in layout.IsNestedBy: |
84 | | - if 0 < len(rel.RelatedObjects): |
85 | | - last_segment = rel.RelatedObjects[1] |
86 | | - last_segment_dist_along = ( |
87 | | - last_segment.DesignParameters.StartDistAlong + last_segment.DesignParameters.HorizontalLength |
88 | | - ) |
89 | | - last_segment_cant_left = ( |
90 | | - last_segment.DesignParameters.EndCantLeft |
91 | | - if last_segment.DesignParameters.EndCantLeft != None |
92 | | - else last_segment.DesignParameters.StartCantLeft |
93 | | - ) |
94 | | - last_segment_cant_right = ( |
95 | | - last_segment.DesignParameters.EndCantRight |
96 | | - if last_segment.DesignParameters.EndCantRight != None |
97 | | - else last_segment.DesignParameters.StartCantRight |
98 | | - ) |
99 | | - |
100 | | - design_parameters = file.createIfcAlignmentCantSegment( |
101 | | - StartDistAlong=last_segment_dist_along, |
102 | | - HorizontalLength=0.0, |
103 | | - StartCantLeft=last_segment_cant_left, |
104 | | - StartCantRight=last_segment_cant_right, |
105 | | - PredefinedType="CONSTANTCANT", |
106 | | - ) |
107 | | - segment = file.createIfcAlignmentSegment(GlobalId=ifcopenshell.guid.new(), DesignParameters=design_parameters) |
108 | | - |
109 | | - ifcopenshell.api.nest.assign_object(file, related_objects=[segment], relating_object=layout) |
| 41 | + |
| 42 | + if (not ifcopenshell.api.alignment.add_zero_length_segment(file,layout,include_referent=False)): |
| 43 | + return # zero length segment not added, probably because it already exists |
110 | 44 |
|
111 | 45 | curve = ifcopenshell.api.alignment.get_layout_curve(layout) |
112 | 46 |
|
113 | | - has_zero_length_segment = False |
114 | | - if curve.Segments != None and 0 < len(curve.Segments): |
115 | | - last_segment = curve.Segments[-1] |
116 | | - has_zero_length_segment = ( |
117 | | - True |
118 | | - if last_segment.Transition == "DISCONTINUOUS" and last_segment.SegmentLength.wrappedValue == 0.0 |
119 | | - else False |
120 | | - ) |
121 | | - |
122 | | - if not has_zero_length_segment: |
123 | | - parent_curve = file.createIfcLine( |
124 | | - Pnt=file.createIfcCartesianPoint(Coordinates=((0.0, 0.0))), |
125 | | - Dir=file.createIfcVector( |
126 | | - Orientation=file.createIfcDirection(DirectionRatios=((1.0, 0.0))), |
127 | | - Magnitude=1.0, |
128 | | - ), |
129 | | - ) |
130 | | - curve_segment = file.createIfcCurveSegment( |
131 | | - Transition="DISCONTINUOUS", |
132 | | - Placement=file.createIfcAxis2Placement2D( |
133 | | - Location=file.createIfcCartesianPoint((0.0, 0.0)), |
134 | | - RefDirection=file.createIfcDirection((1.0, 0.0)), |
135 | | - ), |
136 | | - SegmentStart=file.createIfcLengthMeasure(0.0), |
137 | | - SegmentLength=file.createIfcLengthMeasure(0.0), |
138 | | - ParentCurve=parent_curve, |
139 | | - ) |
140 | | - curve.Segments = [ |
141 | | - curve_segment, |
142 | | - ] |
| 47 | + if curve: |
| 48 | + ifcopenshell.api.alignment.add_zero_length_segment(file,curve) |
143 | 49 |
|
144 | | - name = f"{_get_segment_start_point_label(segment,None)} {ifcopenshell.util.stationing.station_as_string(file,0.0)}" |
145 | | - ifcopenshell.api.alignment.add_stationing_referent(file, segment, 0.0, 0.0, name=name) |
| 50 | + segment = layout.IsNestedBy[0].RelatedObjects[-1] |
| 51 | + alignment = ifcopenshell.api.alignment.get_alignment(layout) |
| 52 | + station = ifcopenshell.api.alignment.get_alignment_station(file,alignment) |
| 53 | + name = f"{_get_segment_start_point_label(segment,None)} {ifcopenshell.util.alignment.station_as_string(file,station)}" |
| 54 | + ifcopenshell.api.alignment.add_stationing_referent(file, segment, 0.0, station, name=name) |
0 commit comments