-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspb_Brep_Patch_in_FreeCAD.py
More file actions
1090 lines (788 loc) · 32.4 KB
/
spb_Brep_Patch_in_FreeCAD.py
File metadata and controls
1090 lines (788 loc) · 32.4 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 _Patch by using Surface::Filling in FreeCAD.
Although Surface::Filling is supposed to support G2 matching, tested results
show that setting G2 produces G0 results. Also, G1 knots exist within the new
surface, making the G2 goal moot in some ways.
This script uses headless FreeCAD ( https://wiki.freecadweb.org/Headless_FreeCAD )
This script been tested on
Rhino 7, 7.13.21348.13001 & TBD on Windows
FreeCAD 0.019.3 (0.019.24267) on Windows
Requirements to run:
1. Rhino 7. Other versions have not been tested. If you try doing so,
please inform me (see below) 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.
TODO:
Do not export redundant breps, as when multiple edges are selected on a face.
For previous input option, should the input be merged with any current input?
Send any questions, comments, or script development service needs to @spb on
the McNeel Forums ( https://discourse.mcneel.com/ )
"""
from __future__ import absolute_import, print_function, unicode_literals
"""
220112-17: Created, starting with "FilletEdge"-in-FreeCAD script.
"""
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
# Variables to modify per OS, installation paths, and desired placement of
# .stp and .py files for communication between Rhino and FreeCAD.
sFCcmd_Path = '"{}"'.format(r"C:\Program Files\FreeCAD 0.19\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"
#
s_FC = []
def fc(s=''): s_FC.append(s)
class Opts:
keys = []
values = {}
names = {}
riOpts = {}
listValues = {}
stickyKeys = {}
key = 'bAutoCompleteLoop'; keys.append(key)
values[key] = True
riOpts[key] = ri.Custom.OptionToggle(values[key], 'No', 'Yes')
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'iFilter'; keys.append(key)
values[key] = 0
names[key] = 'Filter'
listValues[key] = 'EdgesAndWires', 'EdgesOnly', 'WiresOnly'
stickyKeys[key] = '{}({})'.format(key, __file__)
key = 'bG1_NotG0'; keys.append(key)
values[key] = True
names[key] = 'Continuity'
riOpts[key] = ri.Custom.OptionToggle(values[key], 'G0', 'G1')
stickyKeys[key] = '{}({})'.format(key, __file__)
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 = '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:
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]
else:
idxOpt = go.AddOptionList(
englishOptionName=cls.names[key],
listValues=cls.listValues[key],
listCurrentIndex=cls.values[key])
return idxOpt
@classmethod
def setValue(cls, key, idxList=None):
if key in cls.riOpts:
cls.values[key] = cls.riOpts[key].CurrentValue
elif key in cls.listValues:
cls.values[key] = idxList
else:
return
sc.sticky[cls.stickyKeys[key]] = cls.values[key]
def ptsAreClose(ptA, ptB):
return ptA.DistanceTo(ptB) <= sc.doc.ModelAbsoluteTolerance
def sort_crvs_to_closed_loop(crvs_In):
"""
First curve in crvs_In will be the starting curve.
"""
def matches(pt, idxC_Skip):
idxs_Matches = []
bT1s = [] # False for PointAtStart, True for PointAtEnd
for i, crv in enumerate(crvs_In):
if i in idxC_Skip: continue
if ptsAreClose(pt, crv.PointAtStart):
idxs_Matches.append(i)
bT1s.append(False)
continue
if ptsAreClose(pt, crv.PointAtEnd):
idxs_Matches.append(i)
bT1s.append(True)
return idxs_Matches, bT1s
idxCs_Ordered = [0] # Indices of crvs_In.
pt = crvs_In[0].PointAtEnd
while True:
sc.escape_test()
for idxC, crv in enumerate(crvs_In):
if (idxC != 0) and (idxC in idxCs_Ordered): continue
idx_Matches, bT1s = matches(pt, [idxC]+idxCs_Ordered)
if len(idx_Matches) == 0:
if not ptsAreClose(pt, crvs_In[0].PointAtStart):
print(
"Loop doesn't close with start curve.")
return
# Success.
return idxCs_Ordered
if len(idx_Matches) == 1:
idx_Match = idx_Matches[0]
idxCs_Ordered.extend(idx_Matches)
if bT1s[0]:
pt = crvs_In[idx_Match].PointAtStart
else:
pt = crvs_In[idx_Match].PointAtEnd
#sc.doc.Objects.AddCurve(crvs_In[idx_Match]); sc.doc.Views.Redraw()
break # to while loop.
if len(idx_Matches) > 1:
print(
"More than one branch detected.",
"Giving up finding single closed loop.")
return
def midPt(crv):
t = list(crv.DivideByCount(2, includeEnds=False))[0]
return crv.PointAt(t)
def autoSearchForOnlyClosedBoundaryLoop(objref):
"""
"""
crv_Start = objref.Edge()
if crv_Start is None:
crv_Start = objref.Curve()
bWire = True
else:
bWire = False
def closestPt(crv, pt):
return crv.PointAt(crv.ClosestPoint(pt)[1])
oes = rd.ObjectEnumeratorSettings()
oes.NormalObjects = True
oes.LockedObjects = False
oes.IncludeLights = False
oes.IncludeGrips = False
if Opts.values['iFilter'] == 0:
oes.ObjectTypeFilter = rd.ObjectType.Brep | rd.ObjectType.Curve
elif Opts.values['iFilter'] == 1:
oes.ObjectTypeFilter = rd.ObjectType.Brep
elif Opts.values['iFilter'] == 2:
oes.ObjectTypeFilter = rd.ObjectType.Curve
crvs_Filtered = []
gCrvs_Filtered = []
for rdO in sc.doc.Objects.GetObjectList(oes):
if rdO.ObjectType == rd.ObjectType.Curve:
if bWire and rdO.Id == objref.ObjectId: continue
wire = rdO.Geometry
if wire.IsClosed: continue
crvs_Filtered.append(wire)
gCrvs_Filtered.append(rdO.Id)
continue
brep = rdO.Geometry
for iE, edge in enumerate(brep.Edges):
if edge.Valence != rg.EdgeAdjacency.Naked: continue
if not bWire and (rdO.Id == objref.ObjectId) and (iE == crv_Start.EdgeIndex):
continue
crvs_Filtered.append(edge)
gCrvs_Filtered.append(rdO.Id)
#print(len(crvs_Filtered))
def crvsOverlap(crvA, crvB):
if ptsAreClose(crvA.PointAtStart, crvB.PointAtStart):
if ptsAreClose(crvA.PointAtEnd, crvB.PointAtEnd):
if ptsAreClose(midPt(crvA), closestPt(crvB, midPt(crvA))):
return True
elif ptsAreClose(crvA.PointAtStart, crvB.PointAtEnd):
if ptsAreClose(crvA.PointAtEnd, crvB.PointAtStart):
if ptsAreClose(midPt(crvA), closestPt(crvB, midPt(crvA))):
return True
def overlaps(crvs):
idxOverlaps = []
for i in range(len(crvs) - 1):
if i in idxOverlaps: continue
for j in range(i+1, len(crvs)):
if j in idxOverlaps: continue
if crvsOverlap(crvs[i], crvs[j]):
idxOverlaps.extend([i, j])
return idxOverlaps
idxs_Overlaps = overlaps(crvs_Filtered)
#print(idxs_Overlaps)
for i in sorted(set(idxs_Overlaps), reverse=True):
del crvs_Filtered[i]
del gCrvs_Filtered[i]
#print(len(crvs_Filtered))
#print(*crvs_Filtered, sep='\n')
crvs_to_Sort = [crv_Start] + crvs_Filtered
gCrvs_to_Sort = [objref.ObjectId] + gCrvs_Filtered
idxCs_Ordered = sort_crvs_to_closed_loop(crvs_to_Sort)
if idxCs_Ordered is None: return
#print(idxCs_Ordered)
return (
[crvs_Filtered[i] for i in idxCs_Ordered],
[gCrvs_Filtered[i] for i in idxCs_Ordered])
def getInput():
"""
Get Curves, including BrepEdges, with optional input.
"""
sk_PrevSel = 'UsePrevSelection({})({})'.format(__file__, sc.doc.Name)
def getClosedLoopFromObjRefs(objrefs):
rgCs = []
gCs = []
for objref in objrefs:
crv = objref.Edge()
if crv is None:
crv = objref.Curve()
rgCs.append(crv)
gCs.append(objref.ObjectId)
idxs_Sorted = sort_crvs_to_closed_loop(rgCs)
if idxs_Sorted is None:
print("AInput curves do not form a single closed loop.")
return
if len(idxs_Sorted) < objrefs.Count:
print("BInput curves do not form a single closed loop.")
return
return (
[gCs[idx] for idx in idxs_Sorted],
[rgCs[idx] for idx in idxs_Sorted])
go = ri.Custom.GetObject()
go.GeometryFilter = rd.ObjectType.Curve # Curve is also used for brep edges.
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)
idxs_Opt = {}
sc.doc.Objects.UnselectAll()
while True:
go.ClearCommandOptions()
idxs_Opt.clear()
def addOption(key): idxs_Opt[key] = Opts.addOption(go, key)
addOption('iFilter')
addOption('bG1_NotG0')
addOption('bAutoCompleteLoop')
if not Opts.values['bAutoCompleteLoop']:
if (sk_PrevSel in sc.sticky) and sc.sticky[sk_PrevSel]:
key = 'UsePrevSelection'; idxs_Opt[key] = go.AddOption(key)
addOption('fFreeCADTimeoutSecs')
addOption('bEcho')
addOption('bDebug')
if Opts.values['bAutoCompleteLoop']:
go.SetCommandPrompt("Select curve/edge of boundary")
res = go.Get()
else:
go.SetCommandPrompt("Select curves/edges for boundary")
res = go.GetMultiple(minimumNumber=1, maximumNumber=0)
if Opts.values['iFilter'] == 0:
go.GeometryAttributeFilter = (
ri.Custom.GeometryAttributeFilter.BoundaryEdge |
ri.Custom.GeometryAttributeFilter.EdgeCurve |
ri.Custom.GeometryAttributeFilter.OpenCurve |
ri.Custom.GeometryAttributeFilter.WireCurve)
if Opts.values['iFilter'] == 1:
go.GeometryAttributeFilter = (
ri.Custom.GeometryAttributeFilter.BoundaryEdge |
ri.Custom.GeometryAttributeFilter.EdgeCurve |
ri.Custom.GeometryAttributeFilter.OpenCurve)
if Opts.values['iFilter'] == 2:
go.GeometryAttributeFilter = (
ri.Custom.GeometryAttributeFilter.OpenCurve |
ri.Custom.GeometryAttributeFilter.WireCurve)
if res == ri.GetResult.Cancel:
go.Dispose()
return
if res == ri.GetResult.Object:
objrefs = go.Objects()
if Opts.values['bAutoCompleteLoop']:
rc = autoSearchForOnlyClosedBoundaryLoop(objrefs[0])
if rc is None:
sc.doc.Objects.UnselectAll()
print("AutoCompleteLoop failed. Option disabled.")
key = 'bAutoCompleteLoop'
Opts.riOpts[key].CurrentValue = False
Opts.setValue(key)
continue
go.Dispose()
sc.sticky[sk_PrevSel] = None
return rc
rc = getClosedLoopFromObjRefs(objrefs)
if not rc: continue
go.Dispose()
sc.sticky[sk_PrevSel] = objrefs
return rc
if res == ri.GetResult.Number:
num_In = int(go.Number())
if num_In not in (0,1):
print("Numeric input is invalid.")
continue
key = 'bG1_NotG0'
Opts.riOpts[key].CurrentValue = bool(num_In)
Opts.setValue(key)
continue
# An option was selected.
if (
'UsePrevSelection' in idxs_Opt and
go.Option().Index == idxs_Opt['UsePrevSelection']
):
objrefs = sc.sticky[sk_PrevSel]
#print(len(objrefs))
#for objref in objrefs:
# go.AppendToPickList(objref)
# sc.doc.Views.Redraw()
rc = getClosedLoopFromObjRefs(objrefs)
if not rc: continue
go.Dispose()
sc.sticky[sk_PrevSel] = objrefs
return rc
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):
print("in __init__")
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_rgs_for_FC(rgCrvs):
"""
"""
rgObjs_Out = []
for rgC in rgCrvs:
if not isinstance(rgC, rg.BrepEdge):
rgObjs_Out.append(rgC)
continue
# BrepEdge
edge = rgC
if False:
rg.BrepEdge.Brep
rg.BrepEdge.AdjacentFaces()
for iF in edge.AdjacentFaces():
face = edge.Brep.Faces[iF]
rgB_Out = face.DuplicateFace(False)
if not rgB_Out.IsValid:
print("New monoface brep is not valid. Script canceled.")
return
rgObjs_Out.append(rgB_Out)
return rgObjs_Out
def create_FC_code_Point3Ds_to_Vectors(pts_Mids):
fc('V=FreeCAD.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(rgObjs, bDebug=False):
"""
rgObjs: list(Rhino.Geometry.Brep and/or Rhino.Geometry.Curve)
"""
rdObj_MostRecent = sc.doc.Objects.MostRecentObject()
uInt32_MostRecent = rdObj_MostRecent.RuntimeSerialNumber
gObjs_for_STEP = []
for rgObj in rgObjs:
if isinstance(rgObj, rg.Brep):
gObj_for_STEP = sc.doc.Objects.AddBrep(rgObj)
else:
gObj_for_STEP = sc.doc.Objects.AddCurve(rgObj)
if gObj_for_STEP == gObj_for_STEP.Empty:
print("Could not add object for a {} to document.".format(
rgObj.GetType().Name))
sc.doc.Objects.Delete(objectIds=gObjs_for_STEP, quiet=False)
return
rdObjs_toFC = sc.doc.Objects.AllObjectsSince(uInt32_MostRecent)
sc.doc.Objects.UnselectAll()
rc = [sc.doc.Objects.Select(objectId=rdObj.Id) for rdObj in rdObjs_toFC]
scriptToExportStp = " ".join([
"_-Export",
'"{}"'.format(sPath_STEP_to_FC),
"_Schema=AP214AutomotiveDesign",
"_ExportParameterSpaceCurves=No",
"_SplitClosedSurfaces=No",
"_Enter"])
if not Rhino.RhinoApp.RunScript(scriptToExportStp, echo=bDebug): return
rc = [sc.doc.Objects.Delete(objectId=rdObj.Id, quiet=False) for rdObj in rdObjs_toFC]
if None in rc: 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(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_curves_for_border():
#fc('for i, face in enumerate(shape.Faces):')
fc('def index_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()
fc('edges_Border = []')
fc('idx_edges_to_fillet = []')
fc('conts_per_edge = []')
fc()
fc('for iE, edge in enumerate(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 = index_input_mid_match(edge)')
fc(' if idxMid is None:')
fc(' continue')
fc(' edges_Border.append(edge)')
fc(' idx_edges_to_fillet.append(iE+1)') # Part Edge indices are base 1 in FreeCAD.
fc(' conts_per_edge.append(iConts_at_pts[idxMid])')
fc(' if len(edges_Border) == 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 createBrep(rgCrvs_In, iContinuities, 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))
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
rgObjs_ToPatch = create_rgs_for_FC(rgCrvs_In)
if not rgObjs_ToPatch:
return
Rhino.RhinoApp.SetCommandPrompt("Exporting STEP ...")
if not export_to_STEP(rgObjs_ToPatch, bDebug): return
Rhino.RhinoApp.SetCommandPrompt(sCmdPrompt_In)
pts_Mids = [midPt(rgC_In) for rgC_In in rgCrvs_In]
# Internal distance unit of FreeCAD is the 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
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('iConts_at_pts = []')
for iCont in iContinuities:
fc('iConts_at_pts.append({})'.format(iCont))
fc()
fc('shape = Part.Shape()')
fc('shape.read(r"{}")'.format(sPath_STEP_to_FC))
fc('Part.show(shape)')
fc('doc = FreeCAD.ActiveDocument')
fc('doc.recompute()')
# For debugging.
#fc()
#script_FC = "\n".join(s_FC)
#with open(sPath_Script_for_FC, "w") as f:
# f.write(script_FC)
#return True, None
#
fc()
fc()
#create_FC_code_Determine_curves_for_border()
#fc('shapes_Es = [e.Curve.toShape() for e in edges_Border]')
# Add Part::PartFeatures.
fc('face_objs = []')
fc('edge_objs = []')
fc('fevs = [] # PartFeature Face, Edge.Name, Vector (midpoint)')
fc('for i, face in enumerate(shape.Faces):')
fc(' face_obj = doc.addObject("Part::Feature","Face{}".format(i))')
fc(' face_obj.Shape = face')
fc(' face_objs.append(face_obj)')
fc(' for j, edge in enumerate(face_obj.Shape.Edges):')
fc(' midE=edge.valueAt(edge.Curve.parameterAtDistance(edge.Length/2.0, edge.FirstParameter))')
fc(' fevs.append((face_obj, j+1, midE))')
#fc(' edge_obj = doc.addObject("Part::Feature","Edge{} {}".format(i, j))')
#fc(' edge_obj.Shape = edge')
#fc(' edge_objs.append(edge_obj)')
fc()
fc('doc.recompute()')
fc()
fc('fevs_ordered = []')
fc('for i, v in enumerate(midVs):')
fc(' for j, fev in enumerate(fevs):')
fc(' if fev[2].distanceToPoint(v) < 0.01:')
fc(' fevs_ordered.append(fev)')
fc()
fc()
fc('# Patch')
#fc('edge_objs = []')
#fc('for i, edge in enumerate(edges_Border):')
#fc(' edge_obj = doc.addObject("Part::Feature","Edge{}".format(i))')
#fc(' edge_obj.Shape = edge')
#fc(' edge_objs.append(edge_obj)')
#fc()
#fc('doc.recompute()')
fc()
#fc('data_for_featFilling = []')
#fc('for i, eo in enumerate(edges_Border):')
#fc(' data_for_featFilling.append((eo, ("Edge1".format(i))))')
#fc()
fc('patch = doc.addObject("Surface::Filling","Patch")')
fc()
#fc('doc.Patch.Base = doc.Shape')
#fc('patch.BoundaryEdges = edge_objs')
#fc('patch.BoundaryEdges = [(face_objs[i], "Edge1".format(i)) for i in range(len(edge_objs))]')
fc('patch.BoundaryEdges = [(fevs_ordered[i][0], "Edge{}".format(fevs_ordered[i][1])) for i in range(len(fevs_ordered))]')
#fc(' (edge_objs[0], "Edge1"),')
#fc(' (edge_objs[1], "Edge1"),')
#fc(' (edge_objs[2], "Edge1"),')
#fc(' (edge_objs[3], "Edge1"),')
#fc(' (edge_objs[4], "Edge1"),')
#fc(' (edge_objs[5], "Edge1"),')
#fc(' ]')
fc()
#fc('patch.BoundaryEdges = [(edges_Border[0], "Edge1"), (edges_Border[1], "Edge1")]')
#fc('patch.BoundaryFaces = [face_objs[i].Label for i in range(len(face_objs))]')
#fc('patch.BoundaryFaces = [')
#fc(' face_objs[0].Label,')
#fc(' face_objs[1].Label,')
#fc(' face_objs[2].Label,')
#fc(' face_objs[3].Label,')
#fc(' face_objs[4].Label,')
#fc(' face_objs[5].Label,')
#fc(' ]')
fc('patch.BoundaryFaces = ["Face1" for i in range(len(face_objs))]')
fc()
fc('patch.BoundaryOrder = iConts_at_pts')
fc()
fc('doc.recompute()')
fc('doc.Shape.Visibility = False')
fc()
fc()
# Create STEP file for Rhino.
fc('import Import')
fc('Import.export([doc.Patch], r"{}")'.format(sPath_STEP_from_FC))
fc()
fc()
# Create script file.
fc()
script_FC = "\n".join(s_FC)
with open(sPath_Script_for_FC, "w") as f:
f.write(script_FC)
# Send script to headless FreeCAD (FreeCADcmd.exe) via subprocess.
sForConsole = sFCcmd_Path + " RunMode=Script " + '"{}"'.format(sPath_Script_for_FC)
Rhino.RhinoApp.SetCommandPrompt("Running FreeCAD ...")
rc = subprocess_FC(sForConsole, fFreeCADTimeoutSecs=fFreeCADTimeoutSecs, bEcho=bEcho)
Rhino.RhinoApp.SetCommandPrompt(sCmdPrompt_In)
return bool(rc)
def createBrepObject(rhCrvs_In, iContinuities, fFreeCADTimeoutSecs=10.0, bEcho=True, bDebug=False):
"""
Parameters:
rhCrvs_In: list(ObjRef or rg.Curves)
iContinuities: list(int(Geometric continuity order))
fFreeCADTimeoutSecs: float
bEcho: bool
bDebug: bool
"""
sCmdPrompt_In = Rhino.RhinoApp.CommandPrompt
rgObjs_For_FC = []
#for rgC in rhCrvs_In:
# if isinstance(rgC, rg.BrepEdge):
# if not rgC.IsValid:
#if not rdB_In: return
#if not rgB_In.IsValid:
# print("Brep {} is invalid. Fix first. Brep is skipped.".format(gB_In))
# return
if not createBrep(
rhCrvs_In,
iContinuities=iContinuities,
fFreeCADTimeoutSecs=fFreeCADTimeoutSecs,
bEcho=bEcho,
bDebug=bDebug
):
return
rdB_from_FC = import_STEP_into_Rhino(bDebug)
if not rdB_from_FC:
if bEcho:
print("Patch was not imported.")
return
return rdB_from_FC
def main():
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_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 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.