-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_Brep_filletEdges_in_FreeCAD.py
More file actions
1042 lines (754 loc) · 31.9 KB
/
spb_Brep_filletEdges_in_FreeCAD.py
File metadata and controls
1042 lines (754 loc) · 31.9 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
"""
Complement to _FilletEdge.
This script uses headless FreeCAD ( https://wiki.freecadweb.org/Headless_FreeCAD )
The most significant differences from Rhino's _FilletEdge are at the
intersections of non-tangent edges.
Caution: Check the resultant polysurfaces (B-reps) for missing faces.
See script file for setup instructions.
Send any questions, comments, or script development service needs to @spb on
the McNeel Forums ( https://discourse.mcneel.com/ )
"""
#! python 2 Must be on a line less than 32.
from __future__ import absolute_import, division, print_function, unicode_literals
"""
This script was last tested on
- Rhino 8.11.24254.15001, 2024-09-10
Using
- FreeCAD 0.21.2.33771 (Git)
On Windows.
Requirements to run:
1. Rhino 7 or 8. Other versions have not been tested. If you try other versions,
please inform me (see below) of the results.
2. FreeCAD, preferrably the same version as stated above,
https://www.freecadweb.org/downloads.php
installed where Rhino running the script can access it.
3. Modification of the code in the "Variables to modify per OS"... section
below as needed. Notice that as provided, .stp and .py communication files
are created on the current user's Desktop.
211222-220103: Created.
220106: subprocess.Popen now runs in a threading.Timer.
220110: Removed code for PartDesign Fillet since it has less options than Part Fillet.
Added multiple fillet radius input capability similar to _FilletEdge.
220111: Added preview of text dots on edges containing their assigned radius.
Added option to use previous input.
240101: When running on Windows, now searches and automatically sets FreeCAD
version to the latest installed. Also, added command option to change versions
if more than one are available.
240918: To avoid Python process conflicts, the PYTHONHOME environment variable is
now deleted when the script is run in Rhino 8.
TODO:
For previous input option, should the input be merged with any current input?
Maybe?: Allow variable radius fillet input.
FreeCAD only creates variable fillets that have 2 radii and are G1 blended
between the edges so that the target fillet radii can G1 match to
an adjacent fillet at a tangent edge. They are not linearly blended.
"""
import Rhino
import Rhino.DocObjects as rd
import Rhino.Geometry as rg
import Rhino.Input as ri
import scriptcontext as sc
from System import Guid
from System.Drawing import Color
import os
from subprocess import Popen, PIPE
from threading import Timer
if not Rhino.Runtime.HostUtils.RunningOnWindows:
raise Exception(
"Non-Windows OS. Modify the code in class FC_paths, then delete this 'if' code block.")
class FC_paths:
"""
Variables to modify per OS, installation paths, and desired placement of
.stp and .py files for communication between Rhino and FreeCAD.
"""
sFC_Path_Parent = os.environ["ProgramFiles"]
sFC_Vers = [s[8:] for s in os.listdir(sFC_Path_Parent) if "FreeCAD" in s]
if not sFC_Vers:
raise Exception("FreeCAD not found in Program Files.",
"If it is installed, this script needs to be modified to accommodate FreeCAD's path.")
sFreeCADcmd_path = '"{}"'.format(
os.path.join(
sFC_Path_Parent,
"FreeCAD {}".format(sFC_Vers[0]),
"bin\FreeCADcmd.exe"))
_desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
sPath_Script_for_FC = _desktop + r"\to_FC.py"
sPath_STEP_to_FC = _desktop + r"\to_FC.stp"
sPath_STEP_from_FC = _desktop + r"\from_FC.stp"
@classmethod
def setFC_Cmd_path(cls, sFC_Ver):
cls.sFreeCADcmd_path = '"{}"'.format(
os.path.join(
cls.sFC_Path_Parent,
"FreeCAD {}".format(sFC_Ver),
"bin\FreeCADcmd.exe"))
def checkAndDelPYTHONHOME(bDebug=False):
"""
Doing this due to behavior of Rhino 8.
"""
import os
for sVar in ('PYTHONHOME', ):
sEval = "os.environ['{}']".format(sVar)
if sVar in os.environ:
print(sEval,"=",eval(sEval))
del os.environ[sVar]
if sVar in os.environ:
print(sEval,"=",eval(sEval))
del os.environ[sVar]
else:
if bDebug: print("{} doesn't exist.".format(sEval))
else:
if bDebug: print("{} doesn't exist.".format(sEval))
s_FC = []
def fc(s=''): s_FC.append(s)
class Opts:
keys = []
values = {}
names = {}
riOpts = {}
listValues = {}
stickyKeys = {}
key = 'fRadius'; keys.append(key)
values[key] = 1.0
riOpts[key] = ri.Custom.OptionDouble(values[key])
stickyKeys[key] = '{}({})({})'.format(key, __file__, sc.doc.Name)
key = 'fFreeCADTimeoutSecs'; keys.append(key)
values[key] = 10.0
riOpts[key] = ri.Custom.OptionDouble(values[key], setLowerLimit=True, limit=0.0)
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'iFC_ver'; keys.append(key)
listValues[key] = [s.replace('.', 'p') for s in FC_paths.sFC_Vers]
values[key] = len(listValues[key]) - 1
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bEcho'; keys.append(key)
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bDebug'; keys.append(key)
values[key] = False
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
for key in keys:
if key not in names:
names[key] = key[1:]
# Load sticky.
for key in stickyKeys:
if stickyKeys[key] in sc.sticky:
if key in riOpts:
riOpts[key].CurrentValue = values[key] = sc.sticky[stickyKeys[key]]
else:
# For OptionList.
values[key] = sc.sticky[stickyKeys[key]]
@classmethod
def addOption(cls, go, key):
idxOpt = None
if key in cls.riOpts:
if key[0] == 'b':
idxOpt = go.AddOptionToggle(
cls.names[key], cls.riOpts[key])[0]
elif key[0] == 'f':
idxOpt = go.AddOptionDouble(
cls.names[key], cls.riOpts[key])[0]
elif key[0] == 'i':
idxOpt = go.AddOptionInteger(
englishName=cls.names[key], intValue=cls.riOpts[key])[0]
elif key in cls.listValues:
idxOpt = go.AddOptionList(
englishOptionName=cls.names[key],
listValues=cls.listValues[key],
listCurrentIndex=cls.values[key])
else:
print("{} is not a valid key in Opts.".format(key))
return idxOpt
@classmethod
def setValue(cls, key, idxList=None):
if key == 'fRadius':
if cls.riOpts[key].CurrentValue < 2.0*sc.doc.ModelAbsoluteTolerance:
cls.riOpts[key].CurrentValue = 0.0
cls.values[key] = cls.riOpts[key].CurrentValue
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
return
if key in cls.riOpts:
cls.values[key] = cls.riOpts[key].CurrentValue
elif key in cls.listValues:
cls.values[key] = idxList
else:
print("Why is key, {}, here? Value was not set or sticky-saved.".format(key))
return
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
def getInput(bPrevBrepsArePresent):
"""
Get BrepEdges with optional input.
"""
go = ri.Custom.GetObject()
go.SetCommandPrompt("Select edges to fillet")
go.SetCommandPromptDefault("Confirm edges & radius")
go.GeometryFilter = rd.ObjectType.Curve # Curve is also used for brep edges.
go.GeometryAttributeFilter = (
ri.Custom.GeometryAttributeFilter.MatedEdge |
ri.Custom.GeometryAttributeFilter.EdgeCurve)
go.DeselectAllBeforePostSelect = False # So objects won't be deselected on repeats of While loop.
go.EnableClearObjectsOnEntry(False) # Do not clear objects in go_Main on repeats of While loop.
go.EnablePreSelect(False, ignoreUnacceptablePreselectedObjects=True)
go.EnableUnselectObjectsOnExit(False) # Do not unselect object when an option selected, a number is entered, etc.
go.AcceptNumber(enable=True, acceptZero=True)
go.AcceptNothing(True)
idxs_Opt = {}
sc.doc.Objects.UnselectAll()
while True:
go.ClearCommandOptions()
idxs_Opt.clear()
def addOption(key): idxs_Opt[key] = Opts.addOption(go, key)
addOption('fRadius')
if bPrevBrepsArePresent:
idxs_Opt['UsePrevInput'] = go.AddOption('UsePrevInput')
addOption('fFreeCADTimeoutSecs')
if len(FC_paths.sFC_Vers) > 1: addOption('iFC_ver')
addOption('bEcho')
addOption('bDebug')
res = go.GetMultiple(minimumNumber=1, maximumNumber=0)
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Nothing:
go.Dispose()
return []
if res == ri.GetResult.Object:
objrefs = go.Objects()
go.Dispose()
return objrefs
if res == ri.GetResult.Number:
key = 'fRadius'
Opts.riOpts[key].CurrentValue = go.Number()
Opts.setValue(key)
continue
# An option was selected.
if go.Option().Index == idxs_Opt['UsePrevInput']:
go.Dispose()
return 'UsePrevInput'
for key in idxs_Opt:
if go.Option().Index == idxs_Opt[key]:
Opts.setValue(key, go.Option().CurrentListOptionIndex)
break
class DrawRadiusDotsConduit(Rhino.Display.DisplayConduit):
def __init__(self):
self.prs = None
displayMode = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport.DisplayMode
def PostDrawObjects(self, drawEventArgs):
if not self.prs: return
for pt, rad in self.prs:
if rad == 0.0:
continue
rc = drawEventArgs.Display.DrawDot(
worldPosition=pt,
text="{:.{}f}".format(rad, sc.doc.ModelDistanceDisplayPrecision),
dotColor=sc.doc.Layers.CurrentLayer.Color,
textColor=Color.Black if sc.doc.Layers.CurrentLayer.Color != Color.Black else Color.White)
def create_subBs_per_Es_to_fillet(rgB, idxs_Es):
"""
"""
# Don't bother creating subsets when the face count is low.
if rgB.Faces.Count < 13:
return [rgB.DuplicateBrep()], range(rgB.Faces.Count)
iFs = []
for iE in idxs_Es:
for iF in rgB.Edges[iE].AdjacentFaces():
if iF in iFs: continue
iFs.append(iF)
# Add faces adjacent to all faces in iFs so that the faces at the ends
# of the fillets are processed correctly.
iEs_NoFillet = []
for iF in iFs:
for iE in rgB.Faces[iF].AdjacentEdges():
if iE in idxs_Es: continue
if iE in iEs_NoFillet: continue
iEs_NoFillet.append(iE)
for iE in iEs_NoFillet:
for iF_A in rgB.Edges[iE].AdjacentFaces():
if iF_A not in iFs:
iFs.append(iF_A)
# In case there are any faces missing from the sub-Brep.
iEs_SharedVs = []
for iE_Fillet in idxs_Es:
v = rgB.Edges[iE_Fillet].StartVertex
for iE in v.EdgeIndices():
if iE in idxs_Es: continue
if iE in iEs_NoFillet: continue
iEs_SharedVs.append(iE)
v = rgB.Edges[iE_Fillet].EndVertex
for iE in v.EdgeIndices():
if iE in idxs_Es: continue
if iE in iEs_NoFillet: continue
iEs_SharedVs.append(iE)
for iE in iEs_SharedVs:
for iF_V in rgB.Edges[iE].AdjacentFaces():
if iF_V not in iFs:
print("Face[{}] added via Vertex routine".format(iF_V))
iFs.append(iF_V)
print("Face count of Brep: {}".format(rgB.Faces.Count))
print("Face count of sub-Brep: {}".format(len(iFs)))
# Don't bother creating subsets in some cases.
if len(iFs) == rgB.Faces.Count:
return [rgB.DuplicateBrep()], range(rgB.Faces.Count)
if rgB.Faces.Count - len(iFs) < 13:
return [rgB.DuplicateBrep()], range(rgB.Faces.Count)
print("Creating sub-Brep for export.")
rgBs_ToJoin = [rgB.Faces[iF].DuplicateFace(False) for iF in iFs]
join_tol = max(
sc.doc.ModelAbsoluteTolerance,
max([rgB.Edges[iE].Tolerance for iE in idxs_Es]))
return rg.Brep.JoinBreps(rgBs_ToJoin, tolerance=join_tol), iFs
def create_FC_code_Point3Ds_to_Vectors(pts_Mids):
fc('from FreeCAD import Base')
fc('V=Base.Vector')
fc('midVs = []')
for pt in pts_Mids:
fc('midVs.append(V({}, {}, {}))'.format(pt.X, pt.Y, pt.Z))
fc('')
return True
def export_to_STEP(rgBrep, bDebug=False):
rdObj_MostRecent = sc.doc.Objects.MostRecentObject()
uInt32_MostRecent = rdObj_MostRecent.RuntimeSerialNumber
gBrep_for_STEP = sc.doc.Objects.AddBrep(rgBrep)
if gBrep_for_STEP == gBrep_for_STEP.Empty: return
sc.doc.Objects.UnselectAll()
rdB_to_FC = sc.doc.Objects.AllObjectsSince(uInt32_MostRecent)[0]
sc.doc.Objects.Select(objectId=rdB_to_FC.Id)
scriptToExportStp = " ".join([
"_-Export",
'"{}"'.format(FC_paths.sPath_STEP_to_FC),
"_Schema=AP214AutomotiveDesign",
"_ExportParameterSpaceCurves=No",
"_SplitClosedSurfaces=No",
"_Enter"])
if not Rhino.RhinoApp.RunScript(scriptToExportStp, echo=bDebug): return
if not sc.doc.Objects.Delete(rdB_to_FC): return
return True
def import_STEP_into_Rhino(bDebug=False):
"""
"""
rdObj_MostRecent = sc.doc.Objects.MostRecentObject()
uInt32_MostRecent = rdObj_MostRecent.RuntimeSerialNumber
scriptToImportStp = " ".join([
"_-Import",
'"{}"'.format(FC_paths.sPath_STEP_from_FC),
"_JoinSurfaces=Yes",
"_LimitFaces=No",
"_SkipInvisibles=Yes",
"_ShowBadObjectWarning=Yes",
"_ShowNestedBlockWarning=No",
"_EnterEnd"])
Rhino.RhinoApp.SetCommandPrompt("Importing STEP ...")
Rhino.RhinoApp.RunScript(scriptToImportStp, echo=bDebug)
return sc.doc.Objects.AllObjectsSince(uInt32_MostRecent)
def create_FC_code_Determine_edges_to_fillet():
fc('def idx_input_mid_match(edge):')
fc(' try:')
fc(' midE=edge.valueAt(edge.Curve.parameterAtDistance(edge.Length/2.0, edge.FirstParameter))')
fc(' except:')
fc(' print(edge.Curve)')
fc(' Part.show(edge)')
fc(' Part.Vertex(edge.Curve.parameterAtDistance(0.0))')
fc(' 1/0')
fc(' for i, v in enumerate(midVs):')
fc(' if midE.distanceToPoint(v) < 0.01:')
fc(' return i')
fc()
fc('edges_to_fillet = []')
fc('idx_edges_to_fillet = []')
fc('rads_per_idxE = []')
fc()
fc('for iE, edge in enumerate(doc.Fillet.Base.Shape.Edges):')
#fc(' print(edge.Length)')
fc(' if edge.Degenerated:')
fc(' print("Degenerated Edge skipped.".format(edge.Length))')
fc(' continue')
fc(' if edge.Length < 1e-6:')
fc(' print("Edge with length {} skipped.".format(edge.Length))')
fc(' continue')
fc(' if edge.Length < 0.01:')
fc(' print("Edge with length {} skipped.".format(edge.Length))')
fc(' continue')
fc(' idxMid = idx_input_mid_match(edge)')
fc(' if idxMid is None:')
fc(' continue')
fc(' edges_to_fillet.append(edge)')
fc(' idx_edges_to_fillet.append(iE+1)') # Part Edge indices are base 1 in FreeCAD.
fc(' rads_per_idxE.append(rads_per_pts_Mids[idxMid])')
fc(' if len(edges_to_fillet) == len(midVs):')
fc(' break')
fc('else:')
fc(' raise Exception("Not all the input edges were matched to the edges in FreeCAD.")')
fc()
fc()
def subprocess_FC(sArgs, fFreeCADTimeoutSecs=5.0, bEcho=True):
"""
Parameters:
sArgs: str(Although Popen also accepts a list, trying this when calling FreeCADcmd.exe has so far been unnsuccesful.)
Returns on success: True
Returns on fail: False
References:
https://web.archive.org/web/20160709205942/http://www.ironpython.info/index.php?title=Launching_Sub-Processes
https://www.blog.pythonlibrary.org/2016/05/17/python-101-how-to-timeout-a-subprocess/
"""
kill = lambda process: process.kill()
p = Popen(sArgs, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True) # shell=True hides console window.
my_timer = Timer(fFreeCADTimeoutSecs, kill, [p])
bSuccess = False
try:
my_timer.start()
stdout, stderr = p.communicate()
except:
pass
else:
if stderr or p.returncode:
if bEcho:
print("Failed to get FreeCAD to complete the filleting.")
print("stderr from FreeCAD: {}".format(stderr if stderr else "None"))
if p.returncode:
print("returncode: {}".format(p.returncode))
else:
bSuccess = True
finally:
my_timer.cancel()
return bSuccess
def processBrep(rgBrep_In, idxs_Edges, fRadii, fFreeCADTimeoutSecs=10.0, bEcho=True, bDebug=False):
"""
Parameters:
rgBrep_In: ObjRef or tuple(rd.BrepObject or GUID, float(edge index))
idxs_Edges: list(int(EdgeIndex))
fRadii: list(floats in idxs_Edges order)
fFreeCADTimeoutSecs: float
bEcho: bool
bDebug: bool
"""
sCmdPrompt_In = Rhino.RhinoApp.CommandPrompt
if not rgBrep_In.IsValid:
if bEcho:
print("{}-face brep is invalid. Fix first. Brep is skipped.".format(
rgBrep_In.Faces.Count))
return False, None
rgBs_ToFillet, idx_Fs_to_FC = create_subBs_per_Es_to_fillet(rgBrep_In, idxs_Edges)
if not rgBs_ToFillet:
if bEcho:
print("No shells to fillet in a brep.")
return False, None
pts_Mids = []
for iE in idxs_Edges:
ts = list(rgBrep_In.Edges[iE].DivideByCount(2, includeEnds=False))
if not ts:
print("Midpoint of edge[{}] not found.".format(iE))
return False, None
pt = rgBrep_In.Edges[iE].PointAt(ts[0])
pts_Mids.append(pt)
# Internal distance unit of FreeCAD is millimeter.
if sc.doc.ModelUnitSystem != Rhino.UnitSystem.Millimeters:
fScaleUnit = Rhino.RhinoMath.UnitScale(
sc.doc.ModelUnitSystem,
Rhino.UnitSystem.Millimeters) # from, to
# Breps sent to FC via STEP do not need to be scaled.
for iP, pt in enumerate(pts_Mids):
pts_Mids[iP].X = pt.X * fScaleUnit
pts_Mids[iP].Y = pt.Y * fScaleUnit
pts_Mids[iP].Z = pt.Z * fScaleUnit
#fRadius *= fScaleUnit
fRadii = [r*fScaleUnit for r in fRadii]
fc('import FreeCAD as App')
fc('import Part')
fc()
fc()
if not create_FC_code_Point3Ds_to_Vectors(pts_Mids):
raise Exception("Could not create_FC_code_Point3Ds_to_Vectors.")
fc('rads_per_pts_Mids = []')
for r in fRadii:
fc('rads_per_pts_Mids.append({})'.format(r))
for rgB_ToFillet in rgBs_ToFillet:
Rhino.RhinoApp.SetCommandPrompt("Exporting STEP ...")
if not export_to_STEP(rgB_ToFillet, bDebug): return
Rhino.RhinoApp.SetCommandPrompt(sCmdPrompt_In)
fc('shape = Part.Shape()')
fc('shape.read(r"{}")'.format(FC_paths.sPath_STEP_to_FC))
fc('Part.show(shape)')
fc('doc = FreeCAD.ActiveDocument')
#fc('doc.recompute()') # Worked in 0.19.
# For debugging.
#fc()
#script_FC = "\n".join(s_FC)
#with open(FC_paths.sPath_Script_for_FC, "w") as f:
# f.write(script_FC)
#return True, None
#
fc('doc.addObject("Part::Fillet","Fillet")')
fc('doc.Fillet.Base = doc.Shape')
#fc('doc.recompute()') # Worked in 0.19, but raised 'Recompute failed!' in more recent versions. Also, disabled previous recompute since it seems to work without, even in 0.19.
create_FC_code_Determine_edges_to_fillet()
# Fillet.
fc('data_for_featFillet = []')
fc('for i, iE in enumerate(idx_edges_to_fillet):')
fc(' data_for_featFillet.append((iE, rads_per_idxE[i], rads_per_idxE[i]))')
fc()
fc('doc.Fillet.Edges = data_for_featFillet')
fc('doc.recompute()')
fc('doc.Shape.Visibility = False')
fc()
fc()
# Create STEP file for Rhino.
fc('import Import')
fc('Import.export([FreeCAD.ActiveDocument.Fillet], r"{}")'.format(
FC_paths.sPath_STEP_from_FC))
# Create script file.
fc()
script_FC = "\n".join(s_FC)
with open(FC_paths.sPath_Script_for_FC, "w") as f:
f.write(script_FC)
# Send script to headless FreeCAD (FreeCADcmd.exe) via subprocess.
sForConsole = FC_paths.sFreeCADcmd_path + " RunMode=Script " + '"{}"'.format(
FC_paths.sPath_Script_for_FC)
if bDebug: print(sForConsole)
Rhino.RhinoApp.SetCommandPrompt("Running FreeCAD ...")
rc = subprocess_FC(
sForConsole, fFreeCADTimeoutSecs=fFreeCADTimeoutSecs, bEcho=bEcho)
Rhino.RhinoApp.SetCommandPrompt(sCmdPrompt_In)
if bDebug: print(rc)
if not rc: return False, None
def create_subBs_per_Fs_to_remove(rgB, iFs_to_Remove):
"""
"""
rgB_Out = rgB.DuplicateBrep()
for iF in sorted(iFs_to_Remove, reverse=True):
rgB_Out.Faces.RemoveAt(iF)
return rgB_Out
if len(idx_Fs_to_FC) == rgBrep_In.Faces.Count:
rgB_Fs_not_sent_to_FC = None
else:
rgB_Fs_not_sent_to_FC = create_subBs_per_Fs_to_remove(rgBrep_In, idx_Fs_to_FC)
return True, rgB_Fs_not_sent_to_FC
def processBrepObject(rhBrep_In, idxs_Es, fRadii, fFreeCADTimeoutSecs=10.0, bEcho=True, bDebug=False):
"""
Parameters:
rhBrep_In: ObjRef or tuple(rd.BrepObject or GUID, float(edge index))
idxs_Es: list(int(EdgeIndex))
fRadii: list(floats in idxs_Es order)
fFreeCADTimeoutSecs: float
bEcho: bool
bDebug: bool
"""
sCmdPrompt_In = Rhino.RhinoApp.CommandPrompt
rdB_In = None
if isinstance(rhBrep_In, Guid):
gB_In = rhBrep_In
rdB_In = sc.doc.Objects.FindId(gB_In)
elif isinstance(rhBrep_In, rd.BrepObject):
rdB_In = rhBrep_In
gB_In = rdB_In.Id
if not rdB_In: return
rgB_In = rdB_In.BrepGeometry
if not rgB_In.IsValid:
print("Brep {} is invalid. Fix first. Brep is skipped.".format(gB_In))
return
bSuccess, rgB_Fs_not_sent_to_FC = processBrep(
rgB_In,
idxs_Es,
fRadii=fRadii,
fFreeCADTimeoutSecs=fFreeCADTimeoutSecs,
bEcho=bEcho,
bDebug=bDebug)
if not bSuccess: return
rdBs_from_FC = import_STEP_into_Rhino(bDebug)
if not rdBs_from_FC:
if bEcho:
print("Filleted breps were not imported.")
return
Rhino.RhinoApp.SetCommandPrompt(sCmdPrompt_In)
if rgB_Fs_not_sent_to_FC is None:
rgBs_forOut = [rdB.BrepGeometry for rdB in rdBs_from_FC]
else:
join_tol = max(
sc.doc.ModelAbsoluteTolerance,
max([rgB_In.Edges[iE].Tolerance for iE in idxs_Es]))
rgBs_forOut = rg.Brep.JoinBreps(
[rgB_Fs_not_sent_to_FC] + [rdB.BrepGeometry for rdB in rdBs_from_FC],
tolerance=join_tol)
if len(rgBs_forOut) > 1:
if bEcho:
print("More than 1 brep resulted from join.")
if len(rgBs_forOut) == 1:
sc.doc.Objects.Replace(gB_In, rgBs_forOut[0])
if len(rgBs_forOut) > 1:
for rgB_Out in rgBs_forOut:
sc.doc.Objects.AddBrep(rgB_Out, rdB_In.Attributes)
sc.doc.Objects.Delete(rdB_In)
for rdB in rdBs_from_FC:
sc.doc.Objects.Delete(rdB)
def main():
if Rhino.RhinoApp.ExeVersion >= 8:
checkAndDelPYTHONHOME(Opts.values['bDebug'])
gBs_In = [] # This is used as the index for the brep in the following routine.
idx_rgEdges_PerBrep = []
rads_for_idxEs_PerBrep = []
prevSels_Present = []
skey_prevSels = 'prevSels({})'.format(__file__)
if skey_prevSels in sc.sticky:
prevSels_Saved = sc.sticky[skey_prevSels]
iter = rd.ObjectEnumeratorSettings()
iter.NormalObjects = True
iter.LockedObjects = False
iter.IncludeLights = False
iter.IncludeGrips = False
iter.ObjectTypeFilter = rd.ObjectType.Brep
gBs_Saved = [d.Id for d in sc.doc.Objects.GetObjectList(iter)]
for gB, pts, rads in prevSels_Saved:
if gB in gBs_Saved:
prevSels_Present.append((gB, pts, rads))
skey_conduit = 'conduit({})'.format(__file__)
if (skey_conduit in sc.sticky) and sc.sticky[skey_conduit]:
conduit = sc.sticky[skey_conduit]
else:
conduit = DrawRadiusDotsConduit()
sc.sticky[skey_conduit] = conduit
conduit.Enabled = False # Turns off the conduit if left on from a previous execution of this script.
sc.doc.Views.Redraw()
def get_BE_FromInput(rhObj):
if not hasattr(rhObj, '__iter__'):
if not isinstance(rhObj, rd.ObjRef): return
objref = rhObj
gB = objref.ObjectId
idxE = objref.Edge().EdgeIndex
return gB, idxE
if len(rhObj) != 2: return
rhB, idxE = rhObj
if not isinstance(idxE, int): return
if isinstance(rhB, rd.BrepObject):
rdB = rhB
gB = rdB.Id
elif isinstance(rhB, Guid):
gB = rhB
return gB, idxE
def sortInputPerBrep(rhObjs_In):
for rhObj in rhObjs_In:
rc = get_BE_FromInput(rhObj)
if not rc: return
gB, idxE = rc
if gB in gBs_In:
if idxE in idx_rgEdges_PerBrep[gBs_In.index(gB)]:
# Modify radius value.
idxEperB = idx_rgEdges_PerBrep[gBs_In.index(gB)].index(idxE)
rads_for_idxEs_PerBrep[gBs_In.index(gB)][idxEperB] = fRadius
continue
idx_rgEdges_PerBrep[gBs_In.index(gB)].append(idxE)
rads_for_idxEs_PerBrep[gBs_In.index(gB)].append(fRadius)
continue
gBs_In.append(gB)
idx_rgEdges_PerBrep.append([idxE])
rads_for_idxEs_PerBrep.append([fRadius])
def getEdgeIndex_MatchingMidPoint(rgBrep, midpt):
for edge in rgBrep.Edges:
ts = list(edge.DivideByCount(2, includeEnds=False))
if not ts:
print("Midpoint of edge[{}] not found.".format(edge.EdgeIndex))
return
if edge.PointAt(ts[0]).DistanceTo(midpt) < 0.1 * sc.doc.ModelAbsoluteTolerance:
return edge.EdgeIndex
def prepareDataForPreviewAndSticky():
zipped = zip(gBs_In, idx_rgEdges_PerBrep, rads_for_idxEs_PerBrep)
rgBs = []
pts_Mids_All = [] # Won't include matching radius of 0.0.
rads_All = [] # Won't include radius of 0.0.
bprs = [] # list of tuples of (GUID, pts, radii) for saving input for future use of script.
for gB_In, idxs_Es, rads_for_idxEs in zipped:
rdB = sc.doc.Objects.FindId(gB_In)
rgB = rdB.Geometry
rgBs.append(rgB)
pts_Mids_ThisB = [] # Won't include matching radius of 0.0.
rads_ThisB = [] # Won't include radius of 0.0.
for i, rad in enumerate(rads_for_idxEs):
if rad == 0.0:
continue
iE = idxs_Es[i]
ts = list(rgB.Edges[iE].DivideByCount(2, includeEnds=False))
if not ts:
print("Midpoint of edge[{}] not found.".format(iE))
return
pt = rgB.Edges[iE].PointAt(ts[0])
pts_Mids_ThisB.append(pt)
rads_ThisB.append(rad)
if not pts_Mids_ThisB: continue
bprs.append((rdB.Id, pts_Mids_ThisB, rads_ThisB))
pts_Mids_All.extend(pts_Mids_ThisB)
rads_All.extend(rads_ThisB)
sc.sticky[skey_prevSels] = bprs
if not rgBs: return
return pts_Mids_All, rads_All
while True:
rc = getInput(bool(prevSels_Present))
if rc is None:
conduit.Enabled = False
del conduit
del sc.sticky[skey_conduit]
sc.sticky[skey_conduit] = None
return
if rc == 'UsePrevInput':
sc.doc.Objects.UnselectAll()
gBs_In = []
idx_rgEdges_PerBrep = []
rads_for_idxEs_PerBrep = []
for gB, pts, rads in prevSels_Present:
rgB = sc.doc.Objects.FindId(gB).BrepGeometry
idx_rgEdges_ThisB = []
rads_for_idxEs_ThisB = []
for i, pt in enumerate(pts):
idxE = getEdgeIndex_MatchingMidPoint(rgB, pt)
if idxE is None: continue
idx_rgEdges_ThisB.append(idxE)
rads_for_idxEs_ThisB.append(rads[i])
if idx_rgEdges_ThisB:
gBs_In.append(gB)
idx_rgEdges_PerBrep.append(idx_rgEdges_ThisB)
rads_for_idxEs_PerBrep.append(rads_for_idxEs_ThisB)
rc = prepareDataForPreviewAndSticky()
if not rc: continue
pts_Mids_All, rads_All = rc
conduit.prs = zip(pts_Mids_All, rads_All)
conduit.Enabled = True
sc.doc.Views.Redraw()
continue
fRadius = Opts.values['fRadius']
fFreeCADTimeoutSecs = Opts.values['fFreeCADTimeoutSecs']
iFC_ver = Opts.values['iFC_ver']
bEcho = Opts.values['bEcho']
bDebug = Opts.values['bDebug']
sc.doc.Objects.UnselectAll()
sc.doc.Views.Redraw()
if not rc:
conduit.Enabled = False
del conduit
break
objrefs = rc
sortInputPerBrep(objrefs)
rc = prepareDataForPreviewAndSticky()
if not rc: continue