Skip to content

Split a polyline with a certain number of segment. #986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions _unittest/test_02_3D_modeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from _unittest.conftest import pyaedt_unittest_check_desktop_error
from pyaedt.application.Design import DesignCache
from pyaedt.modeler.Modeler import FaceCoordinateSystem
from pyaedt.modeler.Primitives import PolylineSegment

try:
import pytest # noqa: F401
Expand Down Expand Up @@ -486,3 +487,93 @@ def test_48_coordinate_systems_parametric(self):
cs4 = self.aedtapp.modeler.create_coordinate_system(name="CSP4", mode="zxz", phi=43, theta="126deg", psi=0)
tol = 1e-9
assert sum([abs(x1 - x2) for (x1, x2) in zip(cs3.quaternion, cs4.quaternion)]) < tol

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All tests should have assert checks. Is there a way to also check if points have been created correctly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I will do.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxcapodi78 Done. Thanks for the suggestion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxcapodi78 I used the get_oo_property_value() in the test assertion to make sure that the modeler operations were correct.

def test_49_sweep_along_path(self):
self.aedtapp.modeler.set_working_coordinate_system("Global")
first_points = [[1.0, 1.0, 0], [1.0, 2.0, 1.0], [1.0, 3.0, 1.0]]
first_line = self.aedtapp.modeler.create_polyline([[0.0, 0.0, 0.0], first_points[0]])
assert first_line.insert_segment(
position_list=first_points, segment=PolylineSegment("Spline", num_points=3), segment_number=3
)

assert (
self.aedtapp.get_oo_property_value(
self.aedtapp.modeler.oeditor, first_line.name + "\\CreatePolyline:1", "Number of curves"
)
== "2"
)
assert (
self.aedtapp.get_oo_property_value(
self.aedtapp.modeler.oeditor, first_line.name + "\\CreatePolyline:1", "Number of segments"
)
== "0"
)
assert (
self.aedtapp.get_oo_property_value(
self.aedtapp.modeler.oeditor, first_line.name + "\\CreatePolyline:1", "Number of points"
)
== "4"
)

second_points = [[3.0, 2.0, 0], [3.0, 3.0, 1.0], [3.0, 4.0, 1.0]]
second_line = self.aedtapp.modeler.create_polyline([[0, 0, 0], second_points[0]])
assert second_line.insert_segment(
position_list=second_points, segment=PolylineSegment("Spline", num_points=3), segment_number=5
)

assert second_line.insert_segment(
position_list=[[-3.0, 4.0, 1.0], [-3.0, 5.0, 3.0], [-3.0, 6.0, 1.0], [-3.0, 7.0, 2.0], [0, 0, 0]],
segment=PolylineSegment("Spline", num_points=5),
segment_number=3,
)

assert (
self.aedtapp.get_oo_property_value(
self.aedtapp.modeler.oeditor, second_line.name + "\\CreatePolyline:1", "Number of curves"
)
== "3"
)
assert (
self.aedtapp.get_oo_property_value(
self.aedtapp.modeler.oeditor, second_line.name + "\\CreatePolyline:1", "Number of segments"
)
== "0"
)
assert (
self.aedtapp.get_oo_property_value(
self.aedtapp.modeler.oeditor, second_line.name + "\\CreatePolyline:1", "Number of points"
)
== "8"
)

assert (
self.aedtapp.get_oo_property_value(
self.aedtapp.modeler.oeditor, second_line.name + "\\CreatePolyline:1\\Segment0", "Segment Type"
)
== "Spline"
)
assert (
self.aedtapp.get_oo_property_value(
self.aedtapp.modeler.oeditor, second_line.name + "\\CreatePolyline:1\\Segment1", "Segment Type"
)
== "Line"
)
assert (
self.aedtapp.get_oo_property_value(
self.aedtapp.modeler.oeditor, second_line.name + "\\CreatePolyline:1\\Segment2", "Segment Type"
)
== "Spline"
)

assert (
self.aedtapp.get_oo_property_value(
self.aedtapp.modeler.oeditor, second_line.name + "\\CreatePolyline:1\\Segment0", "Number of segments"
)
== "3"
)
assert (
self.aedtapp.get_oo_property_value(
self.aedtapp.modeler.oeditor, second_line.name + "\\CreatePolyline:1\\Segment2", "Number of segments"
)
== "5"
)
30 changes: 28 additions & 2 deletions pyaedt/modeler/Primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ def set_crosssection_properties(
return True

@pyaedt_function_handler()
def insert_segment(self, position_list, segment=None):
def insert_segment(self, position_list, segment=None, segment_number=0):
"""Add a segment to an existing polyline.

Parameters
Expand Down Expand Up @@ -820,19 +820,26 @@ def insert_segment(self, position_list, segment=None):
except:
end_point = []

segment_id = 1
segment_index = 0
num_vertices = len(self.vertices)
for vertex in self.vertices:
if vertex.position == end_point:
if vertex.id == self.vertices[0].id:
if segment_id > 0:
segment_id -= 1
at_start = True
break
elif vertex.position == start_point:
# If start_point=[0, 0, 0] (a list of integers provided by the user), it won't be equal to vertex.position
# that returns a list of float: [0., 0., 0.]. Thus we cast start_point as a list of floats.
elif vertex.position == [float(x) for x in start_point]:
at_start = False
if segment_index > 0:
segment_index -= 1
break
segment_index += 1
id_v = 0

if isinstance(self._segment_types, list):
s_types = [i for i in self._segment_types]
else:
Expand Down Expand Up @@ -874,6 +881,25 @@ def insert_segment(self, position_list, segment=None):
varg1 += seg_str[9:]
self._primitives._oeditor.InsertPolylineSegment(varg1)

if segment.type == "Spline":
varg1 = ["NAME:AllTabs"]
varg2 = ["NAME:Geometry3DPolylineTab"]

varg3 = ["NAME:PropServers"]
varg3.append(self._m_name + ":CreatePolyline:1" + ":Segment" + str(segment_id))
varg2.append(varg3)

varg4 = ["NAME:ChangedProps"]
varg5 = ["NAME:Number of Segments"]
varg5.append("Value:=")
varg5.append(str(segment_number))

varg4.append(varg5)
varg2.append(varg4)
varg1.append(varg2)

self._primitives._oeditor.ChangeProperty(varg1)

return True


Expand Down