import fpformat
def read_input_file():
"""
Prompts the user to select a 3D-points TXT file.
Reads the file contents into a list of strings.
Returns:
txtfile (str): file path
Points_Full (list of str): list of point coordinates as strings
"""
Win = Windows()
CurrentPartVar = CurrentPart()
txtfile = Win.OpenFileDialog('Select 3D Pojnts in TXT,mm','TXT files(*.*)|*.txt*','.txt')
f = open(txtfile, 'r')
datareader2 = f.read()
f.close()
# Convert file lines into a list of coordinate strings
data_into_list = datareader2.replace('\n', ',').split(',')
Points_Full = data_into_list
return txtfile, Points_Full
def get_number_of_sections():
"""
Prompts the user for the number of guide curves and returns it as an integer.
"""
print 'Input Number of Guide Curves (typ - 10), then press ENTER'
NumberOfSections = int(Read())
return NumberOfSections
def define_point_ranges(length):
"""
Given the number of points (length) for each spline,
returns 10 range objects. This part remains
hard-coded to handle exactly 10 guide curves.
"""
Points_1_range = range(0, ((length*3)))
Points_2_range = range(length*3, ((3*2*length)))
Points_3_range = range((3*2*length), ((3*3*length)))
Points_4_range = range((3*3*length), ((3*4*length)))
Points_5_range = range((3*4*length), ((3*5*length)))
Points_6_range = range((3*5*length), ((3*6*length)))
Points_7_range = range((3*6*length), ((3*7*length)))
Points_8_range = range((3*7*length), ((3*8*length)))
Points_9_range = range((3*8*length), ((3*9*length)))
Points_10_range = range((3*9*length), ((3*10*length)))
return (
Points_1_range, Points_2_range, Points_3_range,
Points_4_range, Points_5_range, Points_6_range,
Points_7_range, Points_8_range, Points_9_range,
Points_10_range
)
def extract_float_points(Points_Full, index_range):
"""
Converts a sub-range of Points_Full into a list of floats.
"""
return [float(Points_Full[i]) for i in index_range]
def create_spline_sketch(sketch_name, float_points):
"""
Creates a 3D sketch for a spline:
- Loops over the float_points in sets of three,
fixes them to 4 decimals, and accumulates them.
- Adds a B-spline to the sketch.
- Returns (Path object, some sample points) for plane creation, etc.
"""
Path = CurrentPart().Add3DSketch(sketch_name)
PointsList = []
cycle = range(len(float_points)//3)
# Build the array for AddBspline
for aaa in cycle:
IncX = aaa * 3
IncY = (aaa*3) + 1
IncZ = (aaa*3) + 2
X_val = fpformat.fix(float_points[IncX], 4)
Y_val = fpformat.fix(float_points[IncY], 4)
Z_val = fpformat.fix(float_points[IncZ], 4)
PointsList.extend([X_val, Y_val, Z_val])
# Create the B-spline from the points
Path.AddBspline(PointsList)
# Return sample points for plane references (SP, SP1, SP2 etc.)
# Hard-coded offsets: 0, 150, 300 (just as in the original).
# Note: Make sure the float_points array has enough points.
SP = [
fpformat.fix(float(PointsList[0]),4),
fpformat.fix(float(PointsList[1]),4),
fpformat.fix(float(PointsList[2]),4)
]
SP1 = [
fpformat.fix(float(PointsList[150]),4),
fpformat.fix(float(PointsList[151]),4),
fpformat.fix(float(PointsList[152]),4)
]
SP2 = [
fpformat.fix(float(PointsList[300]),4),
fpformat.fix(float(PointsList[301]),4),
fpformat.fix(float(PointsList[302]),4)
]
return Path, SP, SP1, SP2
def create_reference_points_on_part(SP_name, sp_coords):
"""
Adds a reference point in the current part at the location of sp_coords.
sp_coords should be [x, y, z] as strings or floats convertible to strings.
"""
# Convert each to float if needed, then fix to 4 decimals again
x_str = fpformat.fix(float(sp_coords[0]),4)
y_str = fpformat.fix(float(sp_coords[1]),4)
z_str = fpformat.fix(float(sp_coords[2]),4)
CurrentPart().AddPoint(SP_name, [x_str, y_str, z_str])
def create_planes_and_sketches(SP1, SP2, SP3, SP11, SP12, SP13, SP21, SP22, SP23):
"""
Creates 3 planes from sets of points:
Plane1 from SP1, SP2, SP3
Plane2 from SP11, SP12, SP13
Plane3 from SP21, SP22, SP23
Then creates 2D sketches (S1, S2, S3) on those planes.
Returns:
(Plane1, Plane2, Plane3, S1, S2, S3)
"""
Plane1 = CurrentPart().AddPlane('FirstPlane', SP1, SP2, SP3)
Plane2 = CurrentPart().AddPlane('MiddlePlane', SP11, SP12, SP13)
Plane3 = CurrentPart().AddPlane('LastPlane', SP21, SP22, SP23)
S1 = CurrentPart().AddSketch('FirstSketch', Plane1)
S2 = CurrentPart().AddSketch('MiddleSketch', Plane2)
S3 = CurrentPart().AddSketch('LastSketch', Plane3)
return Plane1, Plane2, Plane3, S1, S2, S3
def create_2D_points_and_lines(S1, S2, S3, First_Sketch, Mid_Sketch, End_Sketch):
"""
Takes the 3 sets of global points (First_Sketch, Mid_Sketch, End_Sketch),
projects them onto the local plane sketches (S1, S2, S3),
then adds lines connecting them in each 2D sketch.
"""
SketchPoints_1 = []
SketchPoints_2 = []
SketchPoints_3 = []
# Project 10 sets of points into S1
for aaa in range(10):
IncX = aaa*3
IncY = (aaa*3)+1
IncZ = (aaa*3)+2
X11 = First_Sketch[IncX]
Y11 = First_Sketch[IncY]
Z11 = First_Sketch[IncZ]
UV1 = S1.GlobaltoPoint(X11, Y11, Z11)
SketchPoints_1.append(UV1)
# Project 10 sets of points into S2
for bbb in range(10):
IncX = bbb*3
IncY = (bbb*3)+1
IncZ = (bbb*3)+2
X22 = fpformat.fix(float(Mid_Sketch[IncX]),4)
Y22 = fpformat.fix(float(Mid_Sketch[IncY]),4)
Z22 = fpformat.fix(float(Mid_Sketch[IncZ]),4)
UV2 = S2.GlobaltoPoint(X22, Y22, Z22)
SketchPoints_2.append(UV2)
# Project 10 sets of points into S3
for ccc in range(10):
IncX = ccc*3
IncY = (ccc*3)+1
IncZ = (ccc*3)+2
X33 = fpformat.fix(float(End_Sketch[IncX]),4)
Y33 = fpformat.fix(float(End_Sketch[IncY]),4)
Z33 = fpformat.fix(float(End_Sketch[IncZ]),4)
UV3 = S3.GlobaltoPoint(X33, Y33, Z33)
SketchPoints_3.append(UV3)
# Flatten the lists for AddLines(), and repeat the first point to close
SketchPoints1 = [element for innerList in SketchPoints_1 for element in innerList]
SketchPoints1.extend([SketchPoints1[0], SketchPoints1[1]])
SketchPoints2 = [element for innerList in SketchPoints_2 for element in innerList]
SketchPoints2.extend([SketchPoints2[0], SketchPoints2[1]])
SketchPoints3 = [element for innerList in SketchPoints_3 for element in innerList]
SketchPoints3.extend([SketchPoints3[0], SketchPoints3[1]])
# Finally, add lines to each sketch
S1.AddLines(SketchPoints1, False)
S2.AddLines(SketchPoints2, False)
S3.AddLines(SketchPoints3, False)
def main():
# -------------------
# Step 1: Read file and parse data
# -------------------
txtfile, Points_Full = read_input_file()
NumberOfSections = get_number_of_sections()
length = (len(Points_Full)/(NumberOfSections*3))
print(length) # Just to match the original code's debug
# -------------------
# Step 2: Define index ranges for each group
# -------------------
(Points_1_range, Points_2_range, Points_3_range,
Points_4_range, Points_5_range, Points_6_range,
Points_7_range, Points_8_range, Points_9_range,
Points_10_range) = define_point_ranges(length)
# -------------------
# Step 3: Build float-lists for each group
# -------------------
Points_1 = extract_float_points(Points_Full, Points_1_range)
Points_2 = extract_float_points(Points_Full, Points_2_range)
Points_3 = extract_float_points(Points_Full, Points_3_range)
Points_4 = extract_float_points(Points_Full, Points_4_range)
Points_5 = extract_float_points(Points_Full, Points_5_range)
Points_6 = extract_float_points(Points_Full, Points_6_range)
Points_7 = extract_float_points(Points_Full, Points_7_range)
Points_8 = extract_float_points(Points_Full, Points_8_range)
Points_9 = extract_float_points(Points_Full, Points_9_range)
Points_10 = extract_float_points(Points_Full, Points_10_range)
# -------------------
# Step 4: Create 3D splines (Paths) from each group
# Also add reference points for each set
# -------------------
Path1, SP1, SP11, SP21 = create_spline_sketch('Spline1', Points_1)
Path2, SP2, SP12, SP22 = create_spline_sketch('Spline2', Points_2)
Path3, SP3, SP13, SP23 = create_spline_sketch('Spline3', Points_3)
Path4, SP4, SP14, SP24 = create_spline_sketch('Spline4', Points_4)
Path5, SP5, SP15, SP25 = create_spline_sketch('Spline5', Points_5)
Path6, SP6, SP16, SP26 = create_spline_sketch('Spline6', Points_6)
Path7, SP7, SP17, SP27 = create_spline_sketch('Spline7', Points_7)
Path8, SP8, SP18, SP28 = create_spline_sketch('Spline8', Points_8)
Path9, SP9, SP19, SP29 = create_spline_sketch('Spline9', Points_9)
Path10, SP10, SP20, SP30 = create_spline_sketch('Spline10', Points_10)
# Add those reference points in the current part (SP1, SP11, etc.)
# Original code: CurrentPart.AddPoint('SP1', SP1) ...
# Here we simply replicate it in a small loop:
create_reference_points_on_part('SP1', SP1)
create_reference_points_on_part('SP11', SP11)
create_reference_points_on_part('SP21', SP21)
create_reference_points_on_part('SP2', SP2)
create_reference_points_on_part('SP12', SP12)
create_reference_points_on_part('SP22', SP22)
create_reference_points_on_part('SP3', SP3)
create_reference_points_on_part('SP13', SP13)
create_reference_points_on_part('SP23', SP23)
create_reference_points_on_part('SP4', SP4)
create_reference_points_on_part('SP14', SP14)
create_reference_points_on_part('SP24', SP24)
create_reference_points_on_part('SP5', SP5)
create_reference_points_on_part('SP15', SP15)
create_reference_points_on_part('SP25', SP25)
create_reference_points_on_part('SP6', SP6)
create_reference_points_on_part('SP16', SP16)
create_reference_points_on_part('SP26', SP26)
create_reference_points_on_part('SP7', SP7)
create_reference_points_on_part('SP17', SP17)
create_reference_points_on_part('SP27', SP27)
create_reference_points_on_part('SP8', SP8)
create_reference_points_on_part('SP18', SP18)
create_reference_points_on_part('SP28', SP28)
create_reference_points_on_part('SP9', SP9)
create_reference_points_on_part('SP19', SP19)
create_reference_points_on_part('SP29', SP29)
create_reference_points_on_part('SP10', SP10)
create_reference_points_on_part('SP20', SP20)
create_reference_points_on_part('SP30', SP30)
# -------------------
# Step 5: Make Planes from the first, middle, last points
# across the first 3 splines
# (Matches your original code exactly)
# -------------------
(Plane1, Plane2, Plane3,
S1, S2, S3) = create_planes_and_sketches(SP1, SP2, SP3,
SP11, SP12, SP13,
SP21, SP22, SP23)
# -------------------
# Step 6: Create point lists for 2D sketches (First, Mid, End)
# Then project them onto the sketches as lines
# -------------------
First_Sketch = SP1 + SP2 + SP3 + SP4 + SP5 + SP6 + SP7 + SP8 + SP9 + SP10
Mid_Sketch = SP11 + SP12 + SP13 + SP14 + SP15 + SP16 + SP17 + SP18 + SP19 + SP20
End_Sketch = SP21 + SP22 + SP23 + SP24 + SP25 + SP26 + SP27 + SP28 + SP29 + SP30
print(First_Sketch) # Matches original code
create_2D_points_and_lines(S1, S2, S3, First_Sketch, Mid_Sketch, End_Sketch)
# -------------------------------
# Entry Point
# -------------------------------
main()