@@ -37,12 +37,20 @@ class OpArea extends CamOp {
3737
3838 // selected area polygons: surfaces and edges
3939 let { devel, edgeangle } = settings . controller ;
40- let groups = [ ] ;
40+ let stack = [ ] ;
41+ let areas = this . areas = [ stack ] ;
4142 let polys = [ ] ;
4243
43- function newGroup ( ) {
44- groups . push ( newSlice ( ) ) ;
45- return groups . peek ( ) ;
44+ function newArea ( ) {
45+ if ( stack . length ) {
46+ stack = [ ] ;
47+ areas . push ( stack ) ;
48+ }
49+ }
50+
51+ function newLayer ( ) {
52+ stack . push ( newSlice ( ) ) ;
53+ return stack . peek ( ) ;
4654 }
4755
4856 // gather area selections
@@ -90,27 +98,31 @@ class OpArea extends CamOp {
9098 let proc = 0 ;
9199 let pinc = 1 / polys . length ;
92100 for ( let area of polys ) {
93- if ( devel ) newGroup ( ) . output ( )
101+ if ( devel ) newLayer ( ) . output ( )
94102 . setLayer ( "area" , { line : 0xff8800 } , false )
95103 . addPolys ( [ area ] ) ;
96104
105+ newArea ( ) ;
106+
97107 if ( mode === 'clear' ) {
98108 let zs = base_util . lerp ( zTop , zBottom , down ) ;
99109 let zroc = 0 ;
100110 let zinc = 1 / zs . length ;
101111 for ( let z of zs ) {
102- let layers = newGroup ( ) . output ( ) ;
112+ let slice = newLayer ( ) ;
113+ let layers = slice . output ( ) ;
103114 let outs = [ ] ;
104115 let clip = [ ] ;
105116 let shadow = shadowAt ( z ) ;
106117 POLY . subtract ( [ area ] , shadow , clip , undefined , undefined , 0 ) ;
107118 POLY . offset ( clip , [ - toolDiam / 2 , - toolOver ] , {
108- count : 1 , outs, flat : true , z, minArea : 0
119+ count : 999 , outs, flat : true , z, minArea : 0
109120 } ) ;
110121 if ( outs . length === 0 ) {
111122 // terminate z descent when no further output possible
112123 break ;
113124 }
125+ slice . camLines = outs ;
114126 zroc += zinc ;
115127 progress ( proc + ( pinc * zroc ) , 'clear' ) ;
116128 if ( devel ) layers
@@ -131,16 +143,51 @@ class OpArea extends CamOp {
131143 }
132144 }
133145
134- addSlices ( groups ) ;
146+ addSlices ( areas . flat ( ) ) ;
135147 }
136148
137149 prepare ( ops , progress ) {
138- let { op, state, pockets } = this ;
150+ let { op, state, areas } = this ;
139151 let { getPrintPoint , pocket, setTool, setSpindle, setTolerance } = ops ;
140152 let { process } = state . settings ;
141153
142154 setTool ( op . tool , op . rate ) ;
143155 setSpindle ( op . spindle ) ;
156+
157+ while ( areas ?. length ) {
158+ let printPoint = getPrintPoint ( ) ;
159+ let min = {
160+ dist : Infinity ,
161+ area : undefined
162+ } ;
163+ for ( let area of areas . filter ( p => ! p . used ) ) {
164+ console . log ( { area } ) ;
165+ let topPolys = area [ 0 ] . camLines ;
166+ if ( ! topPolys ) continue ;
167+ let poly = topPolys . slice ( ) . sort ( ( a , b ) => b . area ( ) - a . area ( ) ) [ 0 ] ;
168+ if ( ! poly ) continue ;
169+ console . log ( { poly, printPoint } ) ;
170+ let find = poly . findClosestPointTo ( printPoint ) ;
171+ if ( find . distance < min . dist ) {
172+ min . area = area ;
173+ min . dist = find . distance ;
174+ }
175+ }
176+ if ( min . area ) {
177+ min . area . used = true ;
178+ console . log ( { area : min . area } ) ;
179+ pocket ( {
180+ cutdir : op . ov_conv ,
181+ depthFirst : process . camDepthFirst ,
182+ easeDown : op . down && process . easeDown ? op . down : 0 ,
183+ progress : ( n , m ) => progress ( n / m , "area" ) ,
184+ slices : min . area . filter ( slice => slice . camLines )
185+ } ) ;
186+ } else {
187+ break ;
188+ }
189+ }
190+
144191 }
145192}
146193
0 commit comments