camlib.py 226 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852
  1. # ########################################################## ##
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 2/5/2014 #
  6. # MIT Licence #
  7. # ########################################################## ##
  8. from io import StringIO
  9. import numpy as np
  10. from numpy import arctan2, Inf, array, sqrt, pi, ceil, sin, cos, dot, float32, \
  11. transpose
  12. from numpy.linalg import solve, norm
  13. import re, sys, os, platform
  14. import math
  15. from copy import deepcopy
  16. import traceback
  17. from decimal import Decimal
  18. from rtree import index as rtindex
  19. from lxml import etree as ET
  20. # See: http://toblerity.org/shapely/manual.html
  21. from shapely.geometry import Polygon, LineString, Point, LinearRing, MultiLineString
  22. from shapely.geometry import MultiPoint, MultiPolygon
  23. from shapely.geometry import box as shply_box
  24. from shapely.ops import cascaded_union, unary_union, polygonize
  25. import shapely.affinity as affinity
  26. from shapely.wkt import loads as sloads
  27. from shapely.wkt import dumps as sdumps
  28. from shapely.geometry.base import BaseGeometry
  29. from shapely.geometry import shape
  30. # needed for legacy mode
  31. # Used for solid polygons in Matplotlib
  32. from descartes.patch import PolygonPatch
  33. import collections
  34. from collections import Iterable
  35. import rasterio
  36. from rasterio.features import shapes
  37. import ezdxf
  38. # TODO: Commented for FlatCAM packaging with cx_freeze
  39. # from scipy.spatial import KDTree, Delaunay
  40. # from scipy.spatial import Delaunay
  41. from flatcamParsers.ParseSVG import *
  42. from flatcamParsers.ParseDXF import *
  43. if platform.architecture()[0] == '64bit':
  44. from ortools.constraint_solver import pywrapcp
  45. from ortools.constraint_solver import routing_enums_pb2
  46. import logging
  47. import FlatCAMApp
  48. import gettext
  49. import FlatCAMTranslation as fcTranslate
  50. import builtins
  51. fcTranslate.apply_language('strings')
  52. log = logging.getLogger('base2')
  53. log.setLevel(logging.DEBUG)
  54. formatter = logging.Formatter('[%(levelname)s] %(message)s')
  55. handler = logging.StreamHandler()
  56. handler.setFormatter(formatter)
  57. log.addHandler(handler)
  58. if '_' not in builtins.__dict__:
  59. _ = gettext.gettext
  60. class ParseError(Exception):
  61. pass
  62. class Geometry(object):
  63. """
  64. Base geometry class.
  65. """
  66. defaults = {
  67. "units": 'in',
  68. "geo_steps_per_circle": 128
  69. }
  70. def __init__(self, geo_steps_per_circle=None):
  71. # Units (in or mm)
  72. self.units = Geometry.defaults["units"]
  73. # Final geometry: MultiPolygon or list (of geometry constructs)
  74. self.solid_geometry = None
  75. # Final geometry: MultiLineString or list (of LineString or Points)
  76. self.follow_geometry = None
  77. # Attributes to be included in serialization
  78. self.ser_attrs = ["units", 'solid_geometry', 'follow_geometry']
  79. # Flattened geometry (list of paths only)
  80. self.flat_geometry = []
  81. # this is the calculated conversion factor when the file units are different than the ones in the app
  82. self.file_units_factor = 1
  83. # Index
  84. self.index = None
  85. self.geo_steps_per_circle = geo_steps_per_circle
  86. # variables to display the percentage of work done
  87. self.geo_len = 0
  88. self.old_disp_number = 0
  89. self.el_count = 0
  90. if self.app.is_legacy is False:
  91. self.temp_shapes = self.app.plotcanvas.new_shape_group()
  92. else:
  93. from flatcamGUI.PlotCanvasLegacy import ShapeCollectionLegacy
  94. self.temp_shapes = ShapeCollectionLegacy(obj=self, app=self.app, name='camlib.geometry')
  95. # if geo_steps_per_circle is None:
  96. # geo_steps_per_circle = int(Geometry.defaults["geo_steps_per_circle"])
  97. # self.geo_steps_per_circle = geo_steps_per_circle
  98. def plot_temp_shapes(self, element, color='red'):
  99. try:
  100. for sub_el in element:
  101. self.plot_temp_shapes(sub_el)
  102. except TypeError: # Element is not iterable...
  103. # self.add_shape(shape=element, color=color, visible=visible, layer=0)
  104. self.temp_shapes.add(tolerance=float(self.app.defaults["global_tolerance"]),
  105. shape=element, color=color, visible=True, layer=0)
  106. def make_index(self):
  107. self.flatten()
  108. self.index = FlatCAMRTree()
  109. for i, g in enumerate(self.flat_geometry):
  110. self.index.insert(i, g)
  111. def add_circle(self, origin, radius):
  112. """
  113. Adds a circle to the object.
  114. :param origin: Center of the circle.
  115. :param radius: Radius of the circle.
  116. :return: None
  117. """
  118. if self.solid_geometry is None:
  119. self.solid_geometry = []
  120. if type(self.solid_geometry) is list:
  121. self.solid_geometry.append(Point(origin).buffer(
  122. radius, int(int(self.geo_steps_per_circle) / 4)))
  123. return
  124. try:
  125. self.solid_geometry = self.solid_geometry.union(Point(origin).buffer(
  126. radius, int(int(self.geo_steps_per_circle) / 4)))
  127. except Exception as e:
  128. log.error("Failed to run union on polygons. %s" % str(e))
  129. return
  130. def add_polygon(self, points):
  131. """
  132. Adds a polygon to the object (by union)
  133. :param points: The vertices of the polygon.
  134. :return: None
  135. """
  136. if self.solid_geometry is None:
  137. self.solid_geometry = []
  138. if type(self.solid_geometry) is list:
  139. self.solid_geometry.append(Polygon(points))
  140. return
  141. try:
  142. self.solid_geometry = self.solid_geometry.union(Polygon(points))
  143. except Exception as e:
  144. log.error("Failed to run union on polygons. %s" % str(e))
  145. return
  146. def add_polyline(self, points):
  147. """
  148. Adds a polyline to the object (by union)
  149. :param points: The vertices of the polyline.
  150. :return: None
  151. """
  152. if self.solid_geometry is None:
  153. self.solid_geometry = []
  154. if type(self.solid_geometry) is list:
  155. self.solid_geometry.append(LineString(points))
  156. return
  157. try:
  158. self.solid_geometry = self.solid_geometry.union(LineString(points))
  159. except Exception as e:
  160. log.error("Failed to run union on polylines. %s" % str(e))
  161. return
  162. def is_empty(self):
  163. if isinstance(self.solid_geometry, BaseGeometry):
  164. return self.solid_geometry.is_empty
  165. if isinstance(self.solid_geometry, list):
  166. return len(self.solid_geometry) == 0
  167. self.app.inform.emit('[ERROR_NOTCL] %s' %
  168. _("self.solid_geometry is neither BaseGeometry or list."))
  169. return
  170. def subtract_polygon(self, points):
  171. """
  172. Subtract polygon from the given object. This only operates on the paths in the original geometry,
  173. i.e. it converts polygons into paths.
  174. :param points: The vertices of the polygon.
  175. :return: none
  176. """
  177. if self.solid_geometry is None:
  178. self.solid_geometry = []
  179. # pathonly should be allways True, otherwise polygons are not subtracted
  180. flat_geometry = self.flatten(pathonly=True)
  181. log.debug("%d paths" % len(flat_geometry))
  182. polygon = Polygon(points)
  183. toolgeo = cascaded_union(polygon)
  184. diffs = []
  185. for target in flat_geometry:
  186. if type(target) == LineString or type(target) == LinearRing:
  187. diffs.append(target.difference(toolgeo))
  188. else:
  189. log.warning("Not implemented.")
  190. self.solid_geometry = cascaded_union(diffs)
  191. def bounds(self):
  192. """
  193. Returns coordinates of rectangular bounds
  194. of geometry: (xmin, ymin, xmax, ymax).
  195. """
  196. # fixed issue of getting bounds only for one level lists of objects
  197. # now it can get bounds for nested lists of objects
  198. log.debug("camlib.Geometry.bounds()")
  199. if self.solid_geometry is None:
  200. log.debug("solid_geometry is None")
  201. return 0, 0, 0, 0
  202. def bounds_rec(obj):
  203. if type(obj) is list:
  204. minx = Inf
  205. miny = Inf
  206. maxx = -Inf
  207. maxy = -Inf
  208. for k in obj:
  209. if type(k) is dict:
  210. for key in k:
  211. minx_, miny_, maxx_, maxy_ = bounds_rec(k[key])
  212. minx = min(minx, minx_)
  213. miny = min(miny, miny_)
  214. maxx = max(maxx, maxx_)
  215. maxy = max(maxy, maxy_)
  216. else:
  217. minx_, miny_, maxx_, maxy_ = bounds_rec(k)
  218. minx = min(minx, minx_)
  219. miny = min(miny, miny_)
  220. maxx = max(maxx, maxx_)
  221. maxy = max(maxy, maxy_)
  222. return minx, miny, maxx, maxy
  223. else:
  224. # it's a Shapely object, return it's bounds
  225. return obj.bounds
  226. if self.multigeo is True:
  227. minx_list = []
  228. miny_list = []
  229. maxx_list = []
  230. maxy_list = []
  231. for tool in self.tools:
  232. minx, miny, maxx, maxy = bounds_rec(self.tools[tool]['solid_geometry'])
  233. minx_list.append(minx)
  234. miny_list.append(miny)
  235. maxx_list.append(maxx)
  236. maxy_list.append(maxy)
  237. return(min(minx_list), min(miny_list), max(maxx_list), max(maxy_list))
  238. else:
  239. bounds_coords = bounds_rec(self.solid_geometry)
  240. return bounds_coords
  241. # try:
  242. # # from here: http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html
  243. # def flatten(l, ltypes=(list, tuple)):
  244. # ltype = type(l)
  245. # l = list(l)
  246. # i = 0
  247. # while i < len(l):
  248. # while isinstance(l[i], ltypes):
  249. # if not l[i]:
  250. # l.pop(i)
  251. # i -= 1
  252. # break
  253. # else:
  254. # l[i:i + 1] = l[i]
  255. # i += 1
  256. # return ltype(l)
  257. #
  258. # log.debug("Geometry->bounds()")
  259. # if self.solid_geometry is None:
  260. # log.debug("solid_geometry is None")
  261. # return 0, 0, 0, 0
  262. #
  263. # if type(self.solid_geometry) is list:
  264. # # TODO: This can be done faster. See comment from Shapely mailing lists.
  265. # if len(self.solid_geometry) == 0:
  266. # log.debug('solid_geometry is empty []')
  267. # return 0, 0, 0, 0
  268. # return cascaded_union(flatten(self.solid_geometry)).bounds
  269. # else:
  270. # return self.solid_geometry.bounds
  271. # except Exception as e:
  272. # self.app.inform.emit("[ERROR_NOTCL] Error cause: %s" % str(e))
  273. # log.debug("Geometry->bounds()")
  274. # if self.solid_geometry is None:
  275. # log.debug("solid_geometry is None")
  276. # return 0, 0, 0, 0
  277. #
  278. # if type(self.solid_geometry) is list:
  279. # # TODO: This can be done faster. See comment from Shapely mailing lists.
  280. # if len(self.solid_geometry) == 0:
  281. # log.debug('solid_geometry is empty []')
  282. # return 0, 0, 0, 0
  283. # return cascaded_union(self.solid_geometry).bounds
  284. # else:
  285. # return self.solid_geometry.bounds
  286. def find_polygon(self, point, geoset=None):
  287. """
  288. Find an object that object.contains(Point(point)) in
  289. poly, which can can be iterable, contain iterable of, or
  290. be itself an implementer of .contains().
  291. :param point: See description
  292. :param geoset: a polygon or list of polygons where to find if the param point is contained
  293. :return: Polygon containing point or None.
  294. """
  295. if geoset is None:
  296. geoset = self.solid_geometry
  297. try: # Iterable
  298. for sub_geo in geoset:
  299. p = self.find_polygon(point, geoset=sub_geo)
  300. if p is not None:
  301. return p
  302. except TypeError: # Non-iterable
  303. try: # Implements .contains()
  304. if isinstance(geoset, LinearRing):
  305. geoset = Polygon(geoset)
  306. if geoset.contains(Point(point)):
  307. return geoset
  308. except AttributeError: # Does not implement .contains()
  309. return None
  310. return None
  311. def get_interiors(self, geometry=None):
  312. interiors = []
  313. if geometry is None:
  314. geometry = self.solid_geometry
  315. # ## If iterable, expand recursively.
  316. try:
  317. for geo in geometry:
  318. interiors.extend(self.get_interiors(geometry=geo))
  319. # ## Not iterable, get the interiors if polygon.
  320. except TypeError:
  321. if type(geometry) == Polygon:
  322. interiors.extend(geometry.interiors)
  323. return interiors
  324. def get_exteriors(self, geometry=None):
  325. """
  326. Returns all exteriors of polygons in geometry. Uses
  327. ``self.solid_geometry`` if geometry is not provided.
  328. :param geometry: Shapely type or list or list of list of such.
  329. :return: List of paths constituting the exteriors
  330. of polygons in geometry.
  331. """
  332. exteriors = []
  333. if geometry is None:
  334. geometry = self.solid_geometry
  335. # ## If iterable, expand recursively.
  336. try:
  337. for geo in geometry:
  338. exteriors.extend(self.get_exteriors(geometry=geo))
  339. # ## Not iterable, get the exterior if polygon.
  340. except TypeError:
  341. if type(geometry) == Polygon:
  342. exteriors.append(geometry.exterior)
  343. return exteriors
  344. def flatten(self, geometry=None, reset=True, pathonly=False):
  345. """
  346. Creates a list of non-iterable linear geometry objects.
  347. Polygons are expanded into its exterior and interiors if specified.
  348. Results are placed in self.flat_geometry
  349. :param geometry: Shapely type or list or list of list of such.
  350. :param reset: Clears the contents of self.flat_geometry.
  351. :param pathonly: Expands polygons into linear elements.
  352. """
  353. if geometry is None:
  354. geometry = self.solid_geometry
  355. if reset:
  356. self.flat_geometry = []
  357. # ## If iterable, expand recursively.
  358. try:
  359. for geo in geometry:
  360. if geo is not None:
  361. self.flatten(geometry=geo,
  362. reset=False,
  363. pathonly=pathonly)
  364. # ## Not iterable, do the actual indexing and add.
  365. except TypeError:
  366. if pathonly and type(geometry) == Polygon:
  367. self.flat_geometry.append(geometry.exterior)
  368. self.flatten(geometry=geometry.interiors,
  369. reset=False,
  370. pathonly=True)
  371. else:
  372. self.flat_geometry.append(geometry)
  373. return self.flat_geometry
  374. # def make2Dstorage(self):
  375. #
  376. # self.flatten()
  377. #
  378. # def get_pts(o):
  379. # pts = []
  380. # if type(o) == Polygon:
  381. # g = o.exterior
  382. # pts += list(g.coords)
  383. # for i in o.interiors:
  384. # pts += list(i.coords)
  385. # else:
  386. # pts += list(o.coords)
  387. # return pts
  388. #
  389. # storage = FlatCAMRTreeStorage()
  390. # storage.get_points = get_pts
  391. # for shape in self.flat_geometry:
  392. # storage.insert(shape)
  393. # return storage
  394. # def flatten_to_paths(self, geometry=None, reset=True):
  395. # """
  396. # Creates a list of non-iterable linear geometry elements and
  397. # indexes them in rtree.
  398. #
  399. # :param geometry: Iterable geometry
  400. # :param reset: Wether to clear (True) or append (False) to self.flat_geometry
  401. # :return: self.flat_geometry, self.flat_geometry_rtree
  402. # """
  403. #
  404. # if geometry is None:
  405. # geometry = self.solid_geometry
  406. #
  407. # if reset:
  408. # self.flat_geometry = []
  409. #
  410. # # ## If iterable, expand recursively.
  411. # try:
  412. # for geo in geometry:
  413. # self.flatten_to_paths(geometry=geo, reset=False)
  414. #
  415. # # ## Not iterable, do the actual indexing and add.
  416. # except TypeError:
  417. # if type(geometry) == Polygon:
  418. # g = geometry.exterior
  419. # self.flat_geometry.append(g)
  420. #
  421. # # ## Add first and last points of the path to the index.
  422. # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[0])
  423. # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[-1])
  424. #
  425. # for interior in geometry.interiors:
  426. # g = interior
  427. # self.flat_geometry.append(g)
  428. # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[0])
  429. # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[-1])
  430. # else:
  431. # g = geometry
  432. # self.flat_geometry.append(g)
  433. # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[0])
  434. # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[-1])
  435. #
  436. # return self.flat_geometry, self.flat_geometry_rtree
  437. def isolation_geometry(self, offset, iso_type=2, corner=None, follow=None, passes=0):
  438. """
  439. Creates contours around geometry at a given
  440. offset distance.
  441. :param offset: Offset distance.
  442. :type offset: float
  443. :param iso_type: type of isolation, can be 0 = exteriors or 1 = interiors or 2 = both (complete)
  444. :param corner: type of corner for the isolation: 0 = round; 1 = square; 2= beveled (line that connects the ends)
  445. :param follow: whether the geometry to be isolated is a follow_geometry
  446. :param passes: current pass out of possible multiple passes for which the isolation is done
  447. :return: The buffered geometry.
  448. :rtype: Shapely.MultiPolygon or Shapely.Polygon
  449. """
  450. if self.app.abort_flag:
  451. # graceful abort requested by the user
  452. raise FlatCAMApp.GracefulException
  453. geo_iso = []
  454. if offset == 0:
  455. if follow:
  456. geo_iso = self.follow_geometry
  457. else:
  458. geo_iso = self.solid_geometry
  459. else:
  460. if follow:
  461. geo_iso = self.follow_geometry
  462. else:
  463. if isinstance(self.solid_geometry, list):
  464. temp_geo = cascaded_union(self.solid_geometry)
  465. else:
  466. temp_geo = self.solid_geometry
  467. # Remember: do not make a buffer for each element in the solid_geometry because it will cut into
  468. # other copper features
  469. # if corner is None:
  470. # geo_iso = temp_geo.buffer(offset, int(int(self.geo_steps_per_circle) / 4))
  471. # else:
  472. # geo_iso = temp_geo.buffer(offset, int(int(self.geo_steps_per_circle) / 4),
  473. # join_style=corner)
  474. # variables to display the percentage of work done
  475. geo_len = 0
  476. try:
  477. for pol in self.solid_geometry:
  478. geo_len += 1
  479. except TypeError:
  480. geo_len = 1
  481. disp_number = 0
  482. old_disp_number = 0
  483. pol_nr = 0
  484. # yet, it can be done by issuing an unary_union in the end, thus getting rid of the overlapping geo
  485. try:
  486. for pol in self.solid_geometry:
  487. if self.app.abort_flag:
  488. # graceful abort requested by the user
  489. raise FlatCAMApp.GracefulException
  490. if corner is None:
  491. geo_iso.append(pol.buffer(offset, int(int(self.geo_steps_per_circle) / 4)))
  492. else:
  493. geo_iso.append(pol.buffer(offset, int(int(self.geo_steps_per_circle) / 4)),
  494. join_style=corner)
  495. pol_nr += 1
  496. disp_number = int(np.interp(pol_nr, [0, geo_len], [0, 100]))
  497. if old_disp_number < disp_number <= 100:
  498. self.app.proc_container.update_view_text(' %s %d: %d%%' %
  499. (_("Pass"), int(passes + 1), int(disp_number)))
  500. old_disp_number = disp_number
  501. except TypeError:
  502. # taking care of the case when the self.solid_geometry is just a single Polygon, not a list or a
  503. # MultiPolygon (not an iterable)
  504. if corner is None:
  505. geo_iso.append(self.solid_geometry.buffer(offset, int(int(self.geo_steps_per_circle) / 4)))
  506. else:
  507. geo_iso.append(self.solid_geometry.buffer(offset, int(int(self.geo_steps_per_circle) / 4)),
  508. join_style=corner)
  509. self.app.proc_container.update_view_text(' %s' % _("Buffering"))
  510. geo_iso = unary_union(geo_iso)
  511. self.app.proc_container.update_view_text('')
  512. # end of replaced block
  513. if follow:
  514. return geo_iso
  515. elif iso_type == 2:
  516. return geo_iso
  517. elif iso_type == 0:
  518. self.app.proc_container.update_view_text(' %s' % _("Get Exteriors"))
  519. return self.get_exteriors(geo_iso)
  520. elif iso_type == 1:
  521. self.app.proc_container.update_view_text(' %s' % _("Get Interiors"))
  522. return self.get_interiors(geo_iso)
  523. else:
  524. log.debug("Geometry.isolation_geometry() --> Type of isolation not supported")
  525. return "fail"
  526. def flatten_list(self, list):
  527. for item in list:
  528. if isinstance(item, Iterable) and not isinstance(item, (str, bytes)):
  529. yield from self.flatten_list(item)
  530. else:
  531. yield item
  532. def import_svg(self, filename, object_type=None, flip=True, units='MM'):
  533. """
  534. Imports shapes from an SVG file into the object's geometry.
  535. :param filename: Path to the SVG file.
  536. :type filename: str
  537. :param object_type: parameter passed further along
  538. :param flip: Flip the vertically.
  539. :type flip: bool
  540. :param units: FlatCAM units
  541. :return: None
  542. """
  543. # Parse into list of shapely objects
  544. svg_tree = ET.parse(filename)
  545. svg_root = svg_tree.getroot()
  546. # Change origin to bottom left
  547. # h = float(svg_root.get('height'))
  548. # w = float(svg_root.get('width'))
  549. h = svgparselength(svg_root.get('height'))[0] # TODO: No units support yet
  550. geos = getsvggeo(svg_root, object_type)
  551. if flip:
  552. geos = [translate(scale(g, 1.0, -1.0, origin=(0, 0)), yoff=h) for g in geos]
  553. # Add to object
  554. if self.solid_geometry is None:
  555. self.solid_geometry = []
  556. if type(self.solid_geometry) is list:
  557. # self.solid_geometry.append(cascaded_union(geos))
  558. if type(geos) is list:
  559. self.solid_geometry += geos
  560. else:
  561. self.solid_geometry.append(geos)
  562. else: # It's shapely geometry
  563. # self.solid_geometry = cascaded_union([self.solid_geometry,
  564. # cascaded_union(geos)])
  565. self.solid_geometry = [self.solid_geometry, geos]
  566. # flatten the self.solid_geometry list for import_svg() to import SVG as Gerber
  567. self.solid_geometry = list(self.flatten_list(self.solid_geometry))
  568. geos_text = getsvgtext(svg_root, object_type, units=units)
  569. if geos_text is not None:
  570. geos_text_f = []
  571. if flip:
  572. # Change origin to bottom left
  573. for i in geos_text:
  574. _, minimy, _, maximy = i.bounds
  575. h2 = (maximy - minimy) * 0.5
  576. geos_text_f.append(translate(scale(i, 1.0, -1.0, origin=(0, 0)), yoff=(h + h2)))
  577. if geos_text_f:
  578. self.solid_geometry = self.solid_geometry + geos_text_f
  579. def import_dxf(self, filename, object_type=None, units='MM'):
  580. """
  581. Imports shapes from an DXF file into the object's geometry.
  582. :param filename: Path to the DXF file.
  583. :type filename: str
  584. :param units: Application units
  585. :type flip: str
  586. :return: None
  587. """
  588. # Parse into list of shapely objects
  589. dxf = ezdxf.readfile(filename)
  590. geos = getdxfgeo(dxf)
  591. # Add to object
  592. if self.solid_geometry is None:
  593. self.solid_geometry = []
  594. if type(self.solid_geometry) is list:
  595. if type(geos) is list:
  596. self.solid_geometry += geos
  597. else:
  598. self.solid_geometry.append(geos)
  599. else: # It's shapely geometry
  600. self.solid_geometry = [self.solid_geometry, geos]
  601. # flatten the self.solid_geometry list for import_dxf() to import DXF as Gerber
  602. self.solid_geometry = list(self.flatten_list(self.solid_geometry))
  603. if self.solid_geometry is not None:
  604. self.solid_geometry = cascaded_union(self.solid_geometry)
  605. else:
  606. return
  607. # commented until this function is ready
  608. # geos_text = getdxftext(dxf, object_type, units=units)
  609. # if geos_text is not None:
  610. # geos_text_f = []
  611. # self.solid_geometry = [self.solid_geometry, geos_text_f]
  612. def import_image(self, filename, flip=True, units='MM', dpi=96, mode='black', mask=[128, 128, 128, 128]):
  613. """
  614. Imports shapes from an IMAGE file into the object's geometry.
  615. :param filename: Path to the IMAGE file.
  616. :type filename: str
  617. :param flip: Flip the object vertically.
  618. :type flip: bool
  619. :param units: FlatCAM units
  620. :param dpi: dots per inch on the imported image
  621. :param mode: how to import the image: as 'black' or 'color'
  622. :param mask: level of detail for the import
  623. :return: None
  624. """
  625. scale_factor = 0.264583333
  626. if units.lower() == 'mm':
  627. scale_factor = 25.4 / dpi
  628. else:
  629. scale_factor = 1 / dpi
  630. geos = []
  631. unscaled_geos = []
  632. with rasterio.open(filename) as src:
  633. # if filename.lower().rpartition('.')[-1] == 'bmp':
  634. # red = green = blue = src.read(1)
  635. # print("BMP")
  636. # elif filename.lower().rpartition('.')[-1] == 'png':
  637. # red, green, blue, alpha = src.read()
  638. # elif filename.lower().rpartition('.')[-1] == 'jpg':
  639. # red, green, blue = src.read()
  640. red = green = blue = src.read(1)
  641. try:
  642. green = src.read(2)
  643. except Exception as e:
  644. pass
  645. try:
  646. blue = src.read(3)
  647. except Exception as e:
  648. pass
  649. if mode == 'black':
  650. mask_setting = red <= mask[0]
  651. total = red
  652. log.debug("Image import as monochrome.")
  653. else:
  654. mask_setting = (red <= mask[1]) + (green <= mask[2]) + (blue <= mask[3])
  655. total = np.zeros(red.shape, dtype=float32)
  656. for band in red, green, blue:
  657. total += band
  658. total /= 3
  659. log.debug("Image import as colored. Thresholds are: R = %s , G = %s, B = %s" %
  660. (str(mask[1]), str(mask[2]), str(mask[3])))
  661. for geom, val in shapes(total, mask=mask_setting):
  662. unscaled_geos.append(shape(geom))
  663. for g in unscaled_geos:
  664. geos.append(scale(g, scale_factor, scale_factor, origin=(0, 0)))
  665. if flip:
  666. geos = [translate(scale(g, 1.0, -1.0, origin=(0, 0))) for g in geos]
  667. # Add to object
  668. if self.solid_geometry is None:
  669. self.solid_geometry = []
  670. if type(self.solid_geometry) is list:
  671. # self.solid_geometry.append(cascaded_union(geos))
  672. if type(geos) is list:
  673. self.solid_geometry += geos
  674. else:
  675. self.solid_geometry.append(geos)
  676. else: # It's shapely geometry
  677. self.solid_geometry = [self.solid_geometry, geos]
  678. # flatten the self.solid_geometry list for import_svg() to import SVG as Gerber
  679. self.solid_geometry = list(self.flatten_list(self.solid_geometry))
  680. self.solid_geometry = cascaded_union(self.solid_geometry)
  681. # self.solid_geometry = MultiPolygon(self.solid_geometry)
  682. # self.solid_geometry = self.solid_geometry.buffer(0.00000001)
  683. # self.solid_geometry = self.solid_geometry.buffer(-0.00000001)
  684. def size(self):
  685. """
  686. Returns (width, height) of rectangular
  687. bounds of geometry.
  688. """
  689. if self.solid_geometry is None:
  690. log.warning("Solid_geometry not computed yet.")
  691. return 0
  692. bounds = self.bounds()
  693. return bounds[2] - bounds[0], bounds[3] - bounds[1]
  694. def get_empty_area(self, boundary=None):
  695. """
  696. Returns the complement of self.solid_geometry within
  697. the given boundary polygon. If not specified, it defaults to
  698. the rectangular bounding box of self.solid_geometry.
  699. """
  700. if boundary is None:
  701. boundary = self.solid_geometry.envelope
  702. return boundary.difference(self.solid_geometry)
  703. def clear_polygon(self, polygon, tooldia, steps_per_circle, overlap=0.15, connect=True, contour=True,
  704. prog_plot=False):
  705. """
  706. Creates geometry inside a polygon for a tool to cover
  707. the whole area.
  708. This algorithm shrinks the edges of the polygon and takes
  709. the resulting edges as toolpaths.
  710. :param polygon: Polygon to clear.
  711. :param tooldia: Diameter of the tool.
  712. :param steps_per_circle: number of linear segments to be used to approximate a circle
  713. :param overlap: Overlap of toolpasses.
  714. :param connect: Draw lines between disjoint segments to
  715. minimize tool lifts.
  716. :param contour: Paint around the edges. Inconsequential in
  717. this painting method.
  718. :param prog_plot: boolean; if Ture use the progressive plotting
  719. :return:
  720. """
  721. # log.debug("camlib.clear_polygon()")
  722. assert type(polygon) == Polygon or type(polygon) == MultiPolygon, \
  723. "Expected a Polygon or MultiPolygon, got %s" % type(polygon)
  724. # ## The toolpaths
  725. # Index first and last points in paths
  726. def get_pts(o):
  727. return [o.coords[0], o.coords[-1]]
  728. geoms = FlatCAMRTreeStorage()
  729. geoms.get_points = get_pts
  730. # Can only result in a Polygon or MultiPolygon
  731. # NOTE: The resulting polygon can be "empty".
  732. current = polygon.buffer((-tooldia / 1.999999), int(int(steps_per_circle) / 4))
  733. if current.area == 0:
  734. # Otherwise, trying to to insert current.exterior == None
  735. # into the FlatCAMStorage will fail.
  736. # print("Area is None")
  737. return None
  738. # current can be a MultiPolygon
  739. try:
  740. for p in current:
  741. geoms.insert(p.exterior)
  742. for i in p.interiors:
  743. geoms.insert(i)
  744. # Not a Multipolygon. Must be a Polygon
  745. except TypeError:
  746. geoms.insert(current.exterior)
  747. for i in current.interiors:
  748. geoms.insert(i)
  749. while True:
  750. if self.app.abort_flag:
  751. # graceful abort requested by the user
  752. raise FlatCAMApp.GracefulException
  753. # Can only result in a Polygon or MultiPolygon
  754. current = current.buffer(-tooldia * (1 - overlap), int(int(steps_per_circle) / 4))
  755. if current.area > 0:
  756. # current can be a MultiPolygon
  757. try:
  758. for p in current:
  759. geoms.insert(p.exterior)
  760. for i in p.interiors:
  761. geoms.insert(i)
  762. if prog_plot:
  763. self.plot_temp_shapes(p)
  764. # Not a Multipolygon. Must be a Polygon
  765. except TypeError:
  766. geoms.insert(current.exterior)
  767. if prog_plot:
  768. self.plot_temp_shapes(current.exterior)
  769. for i in current.interiors:
  770. geoms.insert(i)
  771. if prog_plot:
  772. self.plot_temp_shapes(i)
  773. else:
  774. log.debug("camlib.Geometry.clear_polygon() --> Current Area is zero")
  775. break
  776. if prog_plot:
  777. self.temp_shapes.redraw()
  778. # Optimization: Reduce lifts
  779. if connect:
  780. # log.debug("Reducing tool lifts...")
  781. geoms = Geometry.paint_connect(geoms, polygon, tooldia, int(steps_per_circle))
  782. return geoms
  783. def clear_polygon2(self, polygon_to_clear, tooldia, steps_per_circle, seedpoint=None, overlap=0.15,
  784. connect=True, contour=True, prog_plot=False):
  785. """
  786. Creates geometry inside a polygon for a tool to cover
  787. the whole area.
  788. This algorithm starts with a seed point inside the polygon
  789. and draws circles around it. Arcs inside the polygons are
  790. valid cuts. Finalizes by cutting around the inside edge of
  791. the polygon.
  792. :param polygon_to_clear: Shapely.geometry.Polygon
  793. :param steps_per_circle: how many linear segments to use to approximate a circle
  794. :param tooldia: Diameter of the tool
  795. :param seedpoint: Shapely.geometry.Point or None
  796. :param overlap: Tool fraction overlap bewteen passes
  797. :param connect: Connect disjoint segment to minumize tool lifts
  798. :param contour: Cut countour inside the polygon.
  799. :return: List of toolpaths covering polygon.
  800. :rtype: FlatCAMRTreeStorage | None
  801. :param prog_plot: boolean; if True use the progressive plotting
  802. """
  803. # log.debug("camlib.clear_polygon2()")
  804. # Current buffer radius
  805. radius = tooldia / 2 * (1 - overlap)
  806. # ## The toolpaths
  807. # Index first and last points in paths
  808. def get_pts(o):
  809. return [o.coords[0], o.coords[-1]]
  810. geoms = FlatCAMRTreeStorage()
  811. geoms.get_points = get_pts
  812. # Path margin
  813. path_margin = polygon_to_clear.buffer(-tooldia / 2, int(steps_per_circle / 4))
  814. if path_margin.is_empty or path_margin is None:
  815. return
  816. # Estimate good seedpoint if not provided.
  817. if seedpoint is None:
  818. seedpoint = path_margin.representative_point()
  819. # Grow from seed until outside the box. The polygons will
  820. # never have an interior, so take the exterior LinearRing.
  821. while True:
  822. if self.app.abort_flag:
  823. # graceful abort requested by the user
  824. raise FlatCAMApp.GracefulException
  825. path = Point(seedpoint).buffer(radius, int(steps_per_circle / 4)).exterior
  826. path = path.intersection(path_margin)
  827. # Touches polygon?
  828. if path.is_empty:
  829. break
  830. else:
  831. # geoms.append(path)
  832. # geoms.insert(path)
  833. # path can be a collection of paths.
  834. try:
  835. for p in path:
  836. geoms.insert(p)
  837. if prog_plot:
  838. self.plot_temp_shapes(p)
  839. except TypeError:
  840. geoms.insert(path)
  841. if prog_plot:
  842. self.plot_temp_shapes(path)
  843. if prog_plot:
  844. self.temp_shapes.redraw()
  845. radius += tooldia * (1 - overlap)
  846. # Clean inside edges (contours) of the original polygon
  847. if contour:
  848. outer_edges = [x.exterior for x in autolist(
  849. polygon_to_clear.buffer(-tooldia / 2, int(steps_per_circle / 4)))]
  850. inner_edges = []
  851. # Over resulting polygons
  852. for x in autolist(polygon_to_clear.buffer(-tooldia / 2, int(steps_per_circle / 4))):
  853. for y in x.interiors: # Over interiors of each polygon
  854. inner_edges.append(y)
  855. # geoms += outer_edges + inner_edges
  856. for g in outer_edges + inner_edges:
  857. geoms.insert(g)
  858. if prog_plot:
  859. self.plot_temp_shapes(g)
  860. if prog_plot:
  861. self.temp_shapes.redraw()
  862. # Optimization connect touching paths
  863. # log.debug("Connecting paths...")
  864. # geoms = Geometry.path_connect(geoms)
  865. # Optimization: Reduce lifts
  866. if connect:
  867. # log.debug("Reducing tool lifts...")
  868. geoms = Geometry.paint_connect(geoms, polygon_to_clear, tooldia, steps_per_circle)
  869. return geoms
  870. def clear_polygon3(self, polygon, tooldia, steps_per_circle, overlap=0.15, connect=True, contour=True,
  871. prog_plot=False):
  872. """
  873. Creates geometry inside a polygon for a tool to cover
  874. the whole area.
  875. This algorithm draws horizontal lines inside the polygon.
  876. :param polygon: The polygon being painted.
  877. :type polygon: shapely.geometry.Polygon
  878. :param tooldia: Tool diameter.
  879. :param steps_per_circle: how many linear segments to use to approximate a circle
  880. :param overlap: Tool path overlap percentage.
  881. :param connect: Connect lines to avoid tool lifts.
  882. :param contour: Paint around the edges.
  883. :param prog_plot: boolean; if to use the progressive plotting
  884. :return:
  885. """
  886. # log.debug("camlib.clear_polygon3()")
  887. # ## The toolpaths
  888. # Index first and last points in paths
  889. def get_pts(o):
  890. return [o.coords[0], o.coords[-1]]
  891. geoms = FlatCAMRTreeStorage()
  892. geoms.get_points = get_pts
  893. lines_trimmed = []
  894. # Bounding box
  895. left, bot, right, top = polygon.bounds
  896. margin_poly = polygon.buffer(-tooldia / 1.99999999, (int(steps_per_circle)))
  897. # First line
  898. y = top - tooldia / 1.99999999
  899. while y > bot + tooldia / 1.999999999:
  900. if self.app.abort_flag:
  901. # graceful abort requested by the user
  902. raise FlatCAMApp.GracefulException
  903. line = LineString([(left, y), (right, y)])
  904. line = line.intersection(margin_poly)
  905. lines_trimmed.append(line)
  906. y -= tooldia * (1 - overlap)
  907. if prog_plot:
  908. self.plot_temp_shapes(line)
  909. self.temp_shapes.redraw()
  910. # Last line
  911. y = bot + tooldia / 2
  912. line = LineString([(left, y), (right, y)])
  913. line = line.intersection(margin_poly)
  914. for ll in line:
  915. lines_trimmed.append(ll)
  916. if prog_plot:
  917. self.plot_temp_shapes(line)
  918. # Combine
  919. # linesgeo = unary_union(lines)
  920. # Trim to the polygon
  921. # margin_poly = polygon.buffer(-tooldia / 1.99999999, (int(steps_per_circle)))
  922. # lines_trimmed = linesgeo.intersection(margin_poly)
  923. if prog_plot:
  924. self.temp_shapes.redraw()
  925. lines_trimmed = unary_union(lines_trimmed)
  926. # Add lines to storage
  927. try:
  928. for line in lines_trimmed:
  929. geoms.insert(line)
  930. except TypeError:
  931. # in case lines_trimmed are not iterable (Linestring, LinearRing)
  932. geoms.insert(lines_trimmed)
  933. # Add margin (contour) to storage
  934. if contour:
  935. if isinstance(margin_poly, Polygon):
  936. geoms.insert(margin_poly.exterior)
  937. if prog_plot:
  938. self.plot_temp_shapes(margin_poly.exterior)
  939. for ints in margin_poly.interiors:
  940. geoms.insert(ints)
  941. if prog_plot:
  942. self.plot_temp_shapes(ints)
  943. elif isinstance(margin_poly, MultiPolygon):
  944. for poly in margin_poly:
  945. geoms.insert(poly.exterior)
  946. if prog_plot:
  947. self.plot_temp_shapes(poly.exterior)
  948. for ints in poly.interiors:
  949. geoms.insert(ints)
  950. if prog_plot:
  951. self.plot_temp_shapes(ints)
  952. if prog_plot:
  953. self.temp_shapes.redraw()
  954. # Optimization: Reduce lifts
  955. if connect:
  956. # log.debug("Reducing tool lifts...")
  957. geoms = Geometry.paint_connect(geoms, polygon, tooldia, steps_per_circle)
  958. return geoms
  959. def scale(self, xfactor, yfactor, point=None):
  960. """
  961. Scales all of the object's geometry by a given factor. Override
  962. this method.
  963. :param xfactor: Number by which to scale on X axis.
  964. :type xfactor: float
  965. :param yfactor: Number by which to scale on Y axis.
  966. :type yfactor: float
  967. :param point: point to be used as reference for scaling; a tuple
  968. :return: None
  969. :rtype: None
  970. """
  971. return
  972. def offset(self, vect):
  973. """
  974. Offset the geometry by the given vector. Override this method.
  975. :param vect: (x, y) vector by which to offset the object.
  976. :type vect: tuple
  977. :return: None
  978. """
  979. return
  980. @staticmethod
  981. def paint_connect(storage, boundary, tooldia, steps_per_circle, max_walk=None):
  982. """
  983. Connects paths that results in a connection segment that is
  984. within the paint area. This avoids unnecessary tool lifting.
  985. :param storage: Geometry to be optimized.
  986. :type storage: FlatCAMRTreeStorage
  987. :param boundary: Polygon defining the limits of the paintable area.
  988. :type boundary: Polygon
  989. :param tooldia: Tool diameter.
  990. :rtype tooldia: float
  991. :param steps_per_circle: how many linear segments to use to approximate a circle
  992. :param max_walk: Maximum allowable distance without lifting tool.
  993. :type max_walk: float or None
  994. :return: Optimized geometry.
  995. :rtype: FlatCAMRTreeStorage
  996. """
  997. # If max_walk is not specified, the maximum allowed is
  998. # 10 times the tool diameter
  999. max_walk = max_walk or 10 * tooldia
  1000. # Assuming geolist is a flat list of flat elements
  1001. # ## Index first and last points in paths
  1002. def get_pts(o):
  1003. return [o.coords[0], o.coords[-1]]
  1004. # storage = FlatCAMRTreeStorage()
  1005. # storage.get_points = get_pts
  1006. #
  1007. # for shape in geolist:
  1008. # if shape is not None: # TODO: This shouldn't have happened.
  1009. # # Make LlinearRings into linestrings otherwise
  1010. # # When chaining the coordinates path is messed up.
  1011. # storage.insert(LineString(shape))
  1012. # #storage.insert(shape)
  1013. # ## Iterate over geometry paths getting the nearest each time.
  1014. #optimized_paths = []
  1015. optimized_paths = FlatCAMRTreeStorage()
  1016. optimized_paths.get_points = get_pts
  1017. path_count = 0
  1018. current_pt = (0, 0)
  1019. pt, geo = storage.nearest(current_pt)
  1020. storage.remove(geo)
  1021. geo = LineString(geo)
  1022. current_pt = geo.coords[-1]
  1023. try:
  1024. while True:
  1025. path_count += 1
  1026. # log.debug("Path %d" % path_count)
  1027. pt, candidate = storage.nearest(current_pt)
  1028. storage.remove(candidate)
  1029. candidate = LineString(candidate)
  1030. # If last point in geometry is the nearest
  1031. # then reverse coordinates.
  1032. # but prefer the first one if last == first
  1033. if pt != candidate.coords[0] and pt == candidate.coords[-1]:
  1034. candidate.coords = list(candidate.coords)[::-1]
  1035. # Straight line from current_pt to pt.
  1036. # Is the toolpath inside the geometry?
  1037. walk_path = LineString([current_pt, pt])
  1038. walk_cut = walk_path.buffer(tooldia / 2, int(steps_per_circle / 4))
  1039. if walk_cut.within(boundary) and walk_path.length < max_walk:
  1040. # log.debug("Walk to path #%d is inside. Joining." % path_count)
  1041. # Completely inside. Append...
  1042. geo.coords = list(geo.coords) + list(candidate.coords)
  1043. # try:
  1044. # last = optimized_paths[-1]
  1045. # last.coords = list(last.coords) + list(geo.coords)
  1046. # except IndexError:
  1047. # optimized_paths.append(geo)
  1048. else:
  1049. # Have to lift tool. End path.
  1050. # log.debug("Path #%d not within boundary. Next." % path_count)
  1051. # optimized_paths.append(geo)
  1052. optimized_paths.insert(geo)
  1053. geo = candidate
  1054. current_pt = geo.coords[-1]
  1055. # Next
  1056. # pt, geo = storage.nearest(current_pt)
  1057. except StopIteration: # Nothing left in storage.
  1058. # pass
  1059. optimized_paths.insert(geo)
  1060. return optimized_paths
  1061. @staticmethod
  1062. def path_connect(storage, origin=(0, 0)):
  1063. """
  1064. Simplifies paths in the FlatCAMRTreeStorage storage by
  1065. connecting paths that touch on their enpoints.
  1066. :param storage: Storage containing the initial paths.
  1067. :rtype storage: FlatCAMRTreeStorage
  1068. :return: Simplified storage.
  1069. :rtype: FlatCAMRTreeStorage
  1070. """
  1071. log.debug("path_connect()")
  1072. # ## Index first and last points in paths
  1073. def get_pts(o):
  1074. return [o.coords[0], o.coords[-1]]
  1075. #
  1076. # storage = FlatCAMRTreeStorage()
  1077. # storage.get_points = get_pts
  1078. #
  1079. # for shape in pathlist:
  1080. # if shape is not None: # TODO: This shouldn't have happened.
  1081. # storage.insert(shape)
  1082. path_count = 0
  1083. pt, geo = storage.nearest(origin)
  1084. storage.remove(geo)
  1085. # optimized_geometry = [geo]
  1086. optimized_geometry = FlatCAMRTreeStorage()
  1087. optimized_geometry.get_points = get_pts
  1088. # optimized_geometry.insert(geo)
  1089. try:
  1090. while True:
  1091. path_count += 1
  1092. _, left = storage.nearest(geo.coords[0])
  1093. # If left touches geo, remove left from original
  1094. # storage and append to geo.
  1095. if type(left) == LineString:
  1096. if left.coords[0] == geo.coords[0]:
  1097. storage.remove(left)
  1098. geo.coords = list(geo.coords)[::-1] + list(left.coords)
  1099. continue
  1100. if left.coords[-1] == geo.coords[0]:
  1101. storage.remove(left)
  1102. geo.coords = list(left.coords) + list(geo.coords)
  1103. continue
  1104. if left.coords[0] == geo.coords[-1]:
  1105. storage.remove(left)
  1106. geo.coords = list(geo.coords) + list(left.coords)
  1107. continue
  1108. if left.coords[-1] == geo.coords[-1]:
  1109. storage.remove(left)
  1110. geo.coords = list(geo.coords) + list(left.coords)[::-1]
  1111. continue
  1112. _, right = storage.nearest(geo.coords[-1])
  1113. # If right touches geo, remove left from original
  1114. # storage and append to geo.
  1115. if type(right) == LineString:
  1116. if right.coords[0] == geo.coords[-1]:
  1117. storage.remove(right)
  1118. geo.coords = list(geo.coords) + list(right.coords)
  1119. continue
  1120. if right.coords[-1] == geo.coords[-1]:
  1121. storage.remove(right)
  1122. geo.coords = list(geo.coords) + list(right.coords)[::-1]
  1123. continue
  1124. if right.coords[0] == geo.coords[0]:
  1125. storage.remove(right)
  1126. geo.coords = list(geo.coords)[::-1] + list(right.coords)
  1127. continue
  1128. if right.coords[-1] == geo.coords[0]:
  1129. storage.remove(right)
  1130. geo.coords = list(left.coords) + list(geo.coords)
  1131. continue
  1132. # right is either a LinearRing or it does not connect
  1133. # to geo (nothing left to connect to geo), so we continue
  1134. # with right as geo.
  1135. storage.remove(right)
  1136. if type(right) == LinearRing:
  1137. optimized_geometry.insert(right)
  1138. else:
  1139. # Cannot extend geo any further. Put it away.
  1140. optimized_geometry.insert(geo)
  1141. # Continue with right.
  1142. geo = right
  1143. except StopIteration: # Nothing found in storage.
  1144. optimized_geometry.insert(geo)
  1145. # print path_count
  1146. log.debug("path_count = %d" % path_count)
  1147. return optimized_geometry
  1148. def convert_units(self, units):
  1149. """
  1150. Converts the units of the object to ``units`` by scaling all
  1151. the geometry appropriately. This call ``scale()``. Don't call
  1152. it again in descendents.
  1153. :param units: "IN" or "MM"
  1154. :type units: str
  1155. :return: Scaling factor resulting from unit change.
  1156. :rtype: float
  1157. """
  1158. log.debug("camlib.Geometry.convert_units()")
  1159. if units.upper() == self.units.upper():
  1160. return 1.0
  1161. if units.upper() == "MM":
  1162. factor = 25.4
  1163. elif units.upper() == "IN":
  1164. factor = 1 / 25.4
  1165. else:
  1166. log.error("Unsupported units: %s" % str(units))
  1167. return 1.0
  1168. self.units = units
  1169. self.scale(factor, factor)
  1170. self.file_units_factor = factor
  1171. return factor
  1172. def to_dict(self):
  1173. """
  1174. Returns a representation of the object as a dictionary.
  1175. Attributes to include are listed in ``self.ser_attrs``.
  1176. :return: A dictionary-encoded copy of the object.
  1177. :rtype: dict
  1178. """
  1179. d = {}
  1180. for attr in self.ser_attrs:
  1181. d[attr] = getattr(self, attr)
  1182. return d
  1183. def from_dict(self, d):
  1184. """
  1185. Sets object's attributes from a dictionary.
  1186. Attributes to include are listed in ``self.ser_attrs``.
  1187. This method will look only for only and all the
  1188. attributes in ``self.ser_attrs``. They must all
  1189. be present. Use only for deserializing saved
  1190. objects.
  1191. :param d: Dictionary of attributes to set in the object.
  1192. :type d: dict
  1193. :return: None
  1194. """
  1195. for attr in self.ser_attrs:
  1196. setattr(self, attr, d[attr])
  1197. def union(self):
  1198. """
  1199. Runs a cascaded union on the list of objects in
  1200. solid_geometry.
  1201. :return: None
  1202. """
  1203. self.solid_geometry = [cascaded_union(self.solid_geometry)]
  1204. def export_svg(self, scale_factor=0.00):
  1205. """
  1206. Exports the Geometry Object as a SVG Element
  1207. :return: SVG Element
  1208. """
  1209. # Make sure we see a Shapely Geometry class and not a list
  1210. if str(type(self)) == "<class 'FlatCAMObj.FlatCAMGeometry'>":
  1211. flat_geo = []
  1212. if self.multigeo:
  1213. for tool in self.tools:
  1214. flat_geo += self.flatten(self.tools[tool]['solid_geometry'])
  1215. geom = cascaded_union(flat_geo)
  1216. else:
  1217. geom = cascaded_union(self.flatten())
  1218. else:
  1219. geom = cascaded_union(self.flatten())
  1220. # scale_factor is a multiplication factor for the SVG stroke-width used within shapely's svg export
  1221. # If 0 or less which is invalid then default to 0.05
  1222. # This value appears to work for zooming, and getting the output svg line width
  1223. # to match that viewed on screen with FlatCam
  1224. # MS: I choose a factor of 0.01 so the scale is right for PCB UV film
  1225. if scale_factor <= 0:
  1226. scale_factor = 0.01
  1227. # Convert to a SVG
  1228. svg_elem = geom.svg(scale_factor=scale_factor)
  1229. return svg_elem
  1230. def mirror(self, axis, point):
  1231. """
  1232. Mirrors the object around a specified axis passign through
  1233. the given point.
  1234. :param axis: "X" or "Y" indicates around which axis to mirror.
  1235. :type axis: str
  1236. :param point: [x, y] point belonging to the mirror axis.
  1237. :type point: list
  1238. :return: None
  1239. """
  1240. log.debug("camlib.Geometry.mirror()")
  1241. px, py = point
  1242. xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
  1243. def mirror_geom(obj):
  1244. if type(obj) is list:
  1245. new_obj = []
  1246. for g in obj:
  1247. new_obj.append(mirror_geom(g))
  1248. return new_obj
  1249. else:
  1250. try:
  1251. self.el_count += 1
  1252. disp_number = int(np.interp(self.el_count, [0, self.geo_len], [0, 100]))
  1253. if self.old_disp_number < disp_number <= 100:
  1254. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  1255. self.old_disp_number = disp_number
  1256. return affinity.scale(obj, xscale, yscale, origin=(px, py))
  1257. except AttributeError:
  1258. return obj
  1259. try:
  1260. if self.multigeo is True:
  1261. for tool in self.tools:
  1262. # variables to display the percentage of work done
  1263. self.geo_len = 0
  1264. try:
  1265. for g in self.tools[tool]['solid_geometry']:
  1266. self.geo_len += 1
  1267. except TypeError:
  1268. self.geo_len = 1
  1269. self.old_disp_number = 0
  1270. self.el_count = 0
  1271. self.tools[tool]['solid_geometry'] = mirror_geom(self.tools[tool]['solid_geometry'])
  1272. else:
  1273. # variables to display the percentage of work done
  1274. self.geo_len = 0
  1275. try:
  1276. for g in self.solid_geometry:
  1277. self.geo_len += 1
  1278. except TypeError:
  1279. self.geo_len = 1
  1280. self.old_disp_number = 0
  1281. self.el_count = 0
  1282. self.solid_geometry = mirror_geom(self.solid_geometry)
  1283. self.app.inform.emit('[success] %s...' %
  1284. _('Object was mirrored'))
  1285. except AttributeError:
  1286. self.app.inform.emit('[ERROR_NOTCL] %s' %
  1287. _("Failed to mirror. No object selected"))
  1288. self.app.proc_container.new_text = ''
  1289. def rotate(self, angle, point):
  1290. """
  1291. Rotate an object by an angle (in degrees) around the provided coordinates.
  1292. Parameters
  1293. ----------
  1294. The angle of rotation are specified in degrees (default). Positive angles are
  1295. counter-clockwise and negative are clockwise rotations.
  1296. The point of origin can be a keyword 'center' for the bounding box
  1297. center (default), 'centroid' for the geometry's centroid, a Point object
  1298. or a coordinate tuple (x0, y0).
  1299. See shapely manual for more information:
  1300. http://toblerity.org/shapely/manual.html#affine-transformations
  1301. """
  1302. log.debug("camlib.Geometry.rotate()")
  1303. px, py = point
  1304. def rotate_geom(obj):
  1305. if type(obj) is list:
  1306. new_obj = []
  1307. for g in obj:
  1308. new_obj.append(rotate_geom(g))
  1309. return new_obj
  1310. else:
  1311. try:
  1312. self.el_count += 1
  1313. disp_number = int(np.interp(self.el_count, [0, self.geo_len], [0, 100]))
  1314. if self.old_disp_number < disp_number <= 100:
  1315. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  1316. self.old_disp_number = disp_number
  1317. return affinity.rotate(obj, angle, origin=(px, py))
  1318. except AttributeError:
  1319. return obj
  1320. try:
  1321. if self.multigeo is True:
  1322. for tool in self.tools:
  1323. # variables to display the percentage of work done
  1324. self.geo_len = 0
  1325. try:
  1326. for g in self.tools[tool]['solid_geometry']:
  1327. self.geo_len += 1
  1328. except TypeError:
  1329. self.geo_len = 1
  1330. self.old_disp_number = 0
  1331. self.el_count = 0
  1332. self.tools[tool]['solid_geometry'] = rotate_geom(self.tools[tool]['solid_geometry'])
  1333. else:
  1334. # variables to display the percentage of work done
  1335. self.geo_len = 0
  1336. try:
  1337. for g in self.solid_geometry:
  1338. self.geo_len += 1
  1339. except TypeError:
  1340. self.geo_len = 1
  1341. self.old_disp_number = 0
  1342. self.el_count = 0
  1343. self.solid_geometry = rotate_geom(self.solid_geometry)
  1344. self.app.inform.emit('[success] %s...' %
  1345. _('Object was rotated'))
  1346. except AttributeError:
  1347. self.app.inform.emit('[ERROR_NOTCL] %s' %
  1348. _("Failed to rotate. No object selected"))
  1349. self.app.proc_container.new_text = ''
  1350. def skew(self, angle_x, angle_y, point):
  1351. """
  1352. Shear/Skew the geometries of an object by angles along x and y dimensions.
  1353. Parameters
  1354. ----------
  1355. angle_x, angle_y : float, float
  1356. The shear angle(s) for the x and y axes respectively. These can be
  1357. specified in either degrees (default) or radians by setting
  1358. use_radians=True.
  1359. point: tuple of coordinates (x,y)
  1360. See shapely manual for more information:
  1361. http://toblerity.org/shapely/manual.html#affine-transformations
  1362. """
  1363. log.debug("camlib.Geometry.skew()")
  1364. px, py = point
  1365. def skew_geom(obj):
  1366. if type(obj) is list:
  1367. new_obj = []
  1368. for g in obj:
  1369. new_obj.append(skew_geom(g))
  1370. return new_obj
  1371. else:
  1372. try:
  1373. self.el_count += 1
  1374. disp_number = int(np.interp(self.el_count, [0, self.geo_len], [0, 100]))
  1375. if self.old_disp_number < disp_number <= 100:
  1376. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  1377. self.old_disp_number = disp_number
  1378. return affinity.skew(obj, angle_x, angle_y, origin=(px, py))
  1379. except AttributeError:
  1380. return obj
  1381. try:
  1382. if self.multigeo is True:
  1383. for tool in self.tools:
  1384. # variables to display the percentage of work done
  1385. self.geo_len = 0
  1386. try:
  1387. for g in self.tools[tool]['solid_geometry']:
  1388. self.geo_len += 1
  1389. except TypeError:
  1390. self.geo_len = 1
  1391. self.old_disp_number = 0
  1392. self.el_count = 0
  1393. self.tools[tool]['solid_geometry'] = skew_geom(self.tools[tool]['solid_geometry'])
  1394. else:
  1395. # variables to display the percentage of work done
  1396. self.geo_len = 0
  1397. try:
  1398. for g in self.solid_geometry:
  1399. self.geo_len += 1
  1400. except TypeError:
  1401. self.geo_len = 1
  1402. self.old_disp_number = 0
  1403. self.el_count = 0
  1404. self.solid_geometry = skew_geom(self.solid_geometry)
  1405. self.app.inform.emit('[success] %s...' %
  1406. _('Object was skewed'))
  1407. except AttributeError:
  1408. self.app.inform.emit('[ERROR_NOTCL] %s' %
  1409. _("Failed to skew. No object selected"))
  1410. self.app.proc_container.new_text = ''
  1411. # if type(self.solid_geometry) == list:
  1412. # self.solid_geometry = [affinity.skew(g, angle_x, angle_y, origin=(px, py))
  1413. # for g in self.solid_geometry]
  1414. # else:
  1415. # self.solid_geometry = affinity.skew(self.solid_geometry, angle_x, angle_y,
  1416. # origin=(px, py))
  1417. class ApertureMacro:
  1418. """
  1419. Syntax of aperture macros.
  1420. <AM command>: AM<Aperture macro name>*<Macro content>
  1421. <Macro content>: {{<Variable definition>*}{<Primitive>*}}
  1422. <Variable definition>: $K=<Arithmetic expression>
  1423. <Primitive>: <Primitive code>,<Modifier>{,<Modifier>}|<Comment>
  1424. <Modifier>: $M|< Arithmetic expression>
  1425. <Comment>: 0 <Text>
  1426. """
  1427. # ## Regular expressions
  1428. am1_re = re.compile(r'^%AM([^\*]+)\*(.+)?(%)?$')
  1429. am2_re = re.compile(r'(.*)%$')
  1430. amcomm_re = re.compile(r'^0(.*)')
  1431. amprim_re = re.compile(r'^[1-9].*')
  1432. amvar_re = re.compile(r'^\$([0-9a-zA-z]+)=(.*)')
  1433. def __init__(self, name=None):
  1434. self.name = name
  1435. self.raw = ""
  1436. # ## These below are recomputed for every aperture
  1437. # ## definition, in other words, are temporary variables.
  1438. self.primitives = []
  1439. self.locvars = {}
  1440. self.geometry = None
  1441. def to_dict(self):
  1442. """
  1443. Returns the object in a serializable form. Only the name and
  1444. raw are required.
  1445. :return: Dictionary representing the object. JSON ready.
  1446. :rtype: dict
  1447. """
  1448. return {
  1449. 'name': self.name,
  1450. 'raw': self.raw
  1451. }
  1452. def from_dict(self, d):
  1453. """
  1454. Populates the object from a serial representation created
  1455. with ``self.to_dict()``.
  1456. :param d: Serial representation of an ApertureMacro object.
  1457. :return: None
  1458. """
  1459. for attr in ['name', 'raw']:
  1460. setattr(self, attr, d[attr])
  1461. def parse_content(self):
  1462. """
  1463. Creates numerical lists for all primitives in the aperture
  1464. macro (in ``self.raw``) by replacing all variables by their
  1465. values iteratively and evaluating expressions. Results
  1466. are stored in ``self.primitives``.
  1467. :return: None
  1468. """
  1469. # Cleanup
  1470. self.raw = self.raw.replace('\n', '').replace('\r', '').strip(" *")
  1471. self.primitives = []
  1472. # Separate parts
  1473. parts = self.raw.split('*')
  1474. # ### Every part in the macro ####
  1475. for part in parts:
  1476. # ## Comments. Ignored.
  1477. match = ApertureMacro.amcomm_re.search(part)
  1478. if match:
  1479. continue
  1480. # ## Variables
  1481. # These are variables defined locally inside the macro. They can be
  1482. # numerical constant or defind in terms of previously define
  1483. # variables, which can be defined locally or in an aperture
  1484. # definition. All replacements ocurr here.
  1485. match = ApertureMacro.amvar_re.search(part)
  1486. if match:
  1487. var = match.group(1)
  1488. val = match.group(2)
  1489. # Replace variables in value
  1490. for v in self.locvars:
  1491. # replaced the following line with the next to fix Mentor custom apertures not parsed OK
  1492. # val = re.sub((r'\$'+str(v)+r'(?![0-9a-zA-Z])'), str(self.locvars[v]), val)
  1493. val = val.replace('$' + str(v), str(self.locvars[v]))
  1494. # Make all others 0
  1495. val = re.sub(r'\$[0-9a-zA-Z](?![0-9a-zA-Z])', "0", val)
  1496. # Change x with *
  1497. val = re.sub(r'[xX]', "*", val)
  1498. # Eval() and store.
  1499. self.locvars[var] = eval(val)
  1500. continue
  1501. # ## Primitives
  1502. # Each is an array. The first identifies the primitive, while the
  1503. # rest depend on the primitive. All are strings representing a
  1504. # number and may contain variable definition. The values of these
  1505. # variables are defined in an aperture definition.
  1506. match = ApertureMacro.amprim_re.search(part)
  1507. if match:
  1508. # ## Replace all variables
  1509. for v in self.locvars:
  1510. # replaced the following line with the next to fix Mentor custom apertures not parsed OK
  1511. # part = re.sub(r'\$' + str(v) + r'(?![0-9a-zA-Z])', str(self.locvars[v]), part)
  1512. part = part.replace('$' + str(v), str(self.locvars[v]))
  1513. # Make all others 0
  1514. part = re.sub(r'\$[0-9a-zA-Z](?![0-9a-zA-Z])', "0", part)
  1515. # Change x with *
  1516. part = re.sub(r'[xX]', "*", part)
  1517. # ## Store
  1518. elements = part.split(",")
  1519. self.primitives.append([eval(x) for x in elements])
  1520. continue
  1521. log.warning("Unknown syntax of aperture macro part: %s" % str(part))
  1522. def append(self, data):
  1523. """
  1524. Appends a string to the raw macro.
  1525. :param data: Part of the macro.
  1526. :type data: str
  1527. :return: None
  1528. """
  1529. self.raw += data
  1530. @staticmethod
  1531. def default2zero(n, mods):
  1532. """
  1533. Pads the ``mods`` list with zeros resulting in an
  1534. list of length n.
  1535. :param n: Length of the resulting list.
  1536. :type n: int
  1537. :param mods: List to be padded.
  1538. :type mods: list
  1539. :return: Zero-padded list.
  1540. :rtype: list
  1541. """
  1542. x = [0.0] * n
  1543. na = len(mods)
  1544. x[0:na] = mods
  1545. return x
  1546. @staticmethod
  1547. def make_circle(mods):
  1548. """
  1549. :param mods: (Exposure 0/1, Diameter >=0, X-coord, Y-coord)
  1550. :return:
  1551. """
  1552. pol, dia, x, y = ApertureMacro.default2zero(4, mods)
  1553. return {"pol": int(pol), "geometry": Point(x, y).buffer(dia/2)}
  1554. @staticmethod
  1555. def make_vectorline(mods):
  1556. """
  1557. :param mods: (Exposure 0/1, Line width >= 0, X-start, Y-start, X-end, Y-end,
  1558. rotation angle around origin in degrees)
  1559. :return:
  1560. """
  1561. pol, width, xs, ys, xe, ye, angle = ApertureMacro.default2zero(7, mods)
  1562. line = LineString([(xs, ys), (xe, ye)])
  1563. box = line.buffer(width/2, cap_style=2)
  1564. box_rotated = affinity.rotate(box, angle, origin=(0, 0))
  1565. return {"pol": int(pol), "geometry": box_rotated}
  1566. @staticmethod
  1567. def make_centerline(mods):
  1568. """
  1569. :param mods: (Exposure 0/1, width >=0, height >=0, x-center, y-center,
  1570. rotation angle around origin in degrees)
  1571. :return:
  1572. """
  1573. pol, width, height, x, y, angle = ApertureMacro.default2zero(6, mods)
  1574. box = shply_box(x-width/2, y-height/2, x+width/2, y+height/2)
  1575. box_rotated = affinity.rotate(box, angle, origin=(0, 0))
  1576. return {"pol": int(pol), "geometry": box_rotated}
  1577. @staticmethod
  1578. def make_lowerleftline(mods):
  1579. """
  1580. :param mods: (exposure 0/1, width >=0, height >=0, x-lowerleft, y-lowerleft,
  1581. rotation angle around origin in degrees)
  1582. :return:
  1583. """
  1584. pol, width, height, x, y, angle = ApertureMacro.default2zero(6, mods)
  1585. box = shply_box(x, y, x+width, y+height)
  1586. box_rotated = affinity.rotate(box, angle, origin=(0, 0))
  1587. return {"pol": int(pol), "geometry": box_rotated}
  1588. @staticmethod
  1589. def make_outline(mods):
  1590. """
  1591. :param mods:
  1592. :return:
  1593. """
  1594. pol = mods[0]
  1595. n = mods[1]
  1596. points = [(0, 0)]*(n+1)
  1597. for i in range(n+1):
  1598. points[i] = mods[2*i + 2:2*i + 4]
  1599. angle = mods[2*n + 4]
  1600. poly = Polygon(points)
  1601. poly_rotated = affinity.rotate(poly, angle, origin=(0, 0))
  1602. return {"pol": int(pol), "geometry": poly_rotated}
  1603. @staticmethod
  1604. def make_polygon(mods):
  1605. """
  1606. Note: Specs indicate that rotation is only allowed if the center
  1607. (x, y) == (0, 0). I will tolerate breaking this rule.
  1608. :param mods: (exposure 0/1, n_verts 3<=n<=12, x-center, y-center,
  1609. diameter of circumscribed circle >=0, rotation angle around origin)
  1610. :return:
  1611. """
  1612. pol, nverts, x, y, dia, angle = ApertureMacro.default2zero(6, mods)
  1613. points = [(0, 0)]*nverts
  1614. for i in range(nverts):
  1615. points[i] = (x + 0.5 * dia * cos(2*pi * i/nverts),
  1616. y + 0.5 * dia * sin(2*pi * i/nverts))
  1617. poly = Polygon(points)
  1618. poly_rotated = affinity.rotate(poly, angle, origin=(0, 0))
  1619. return {"pol": int(pol), "geometry": poly_rotated}
  1620. @staticmethod
  1621. def make_moire(mods):
  1622. """
  1623. Note: Specs indicate that rotation is only allowed if the center
  1624. (x, y) == (0, 0). I will tolerate breaking this rule.
  1625. :param mods: (x-center, y-center, outer_dia_outer_ring, ring thickness,
  1626. gap, max_rings, crosshair_thickness, crosshair_len, rotation
  1627. angle around origin in degrees)
  1628. :return:
  1629. """
  1630. x, y, dia, thickness, gap, nrings, cross_th, cross_len, angle = ApertureMacro.default2zero(9, mods)
  1631. r = dia/2 - thickness/2
  1632. result = Point((x, y)).buffer(r).exterior.buffer(thickness/2.0)
  1633. ring = Point((x, y)).buffer(r).exterior.buffer(thickness/2.0) # Need a copy!
  1634. i = 1 # Number of rings created so far
  1635. # ## If the ring does not have an interior it means that it is
  1636. # ## a disk. Then stop.
  1637. while len(ring.interiors) > 0 and i < nrings:
  1638. r -= thickness + gap
  1639. if r <= 0:
  1640. break
  1641. ring = Point((x, y)).buffer(r).exterior.buffer(thickness/2.0)
  1642. result = cascaded_union([result, ring])
  1643. i += 1
  1644. # ## Crosshair
  1645. hor = LineString([(x - cross_len, y), (x + cross_len, y)]).buffer(cross_th/2.0, cap_style=2)
  1646. ver = LineString([(x, y-cross_len), (x, y + cross_len)]).buffer(cross_th/2.0, cap_style=2)
  1647. result = cascaded_union([result, hor, ver])
  1648. return {"pol": 1, "geometry": result}
  1649. @staticmethod
  1650. def make_thermal(mods):
  1651. """
  1652. Note: Specs indicate that rotation is only allowed if the center
  1653. (x, y) == (0, 0). I will tolerate breaking this rule.
  1654. :param mods: [x-center, y-center, diameter-outside, diameter-inside,
  1655. gap-thickness, rotation angle around origin]
  1656. :return:
  1657. """
  1658. x, y, dout, din, t, angle = ApertureMacro.default2zero(6, mods)
  1659. ring = Point((x, y)).buffer(dout/2.0).difference(Point((x, y)).buffer(din/2.0))
  1660. hline = LineString([(x - dout/2.0, y), (x + dout/2.0, y)]).buffer(t/2.0, cap_style=3)
  1661. vline = LineString([(x, y - dout/2.0), (x, y + dout/2.0)]).buffer(t/2.0, cap_style=3)
  1662. thermal = ring.difference(hline.union(vline))
  1663. return {"pol": 1, "geometry": thermal}
  1664. def make_geometry(self, modifiers):
  1665. """
  1666. Runs the macro for the given modifiers and generates
  1667. the corresponding geometry.
  1668. :param modifiers: Modifiers (parameters) for this macro
  1669. :type modifiers: list
  1670. :return: Shapely geometry
  1671. :rtype: shapely.geometry.polygon
  1672. """
  1673. # ## Primitive makers
  1674. makers = {
  1675. "1": ApertureMacro.make_circle,
  1676. "2": ApertureMacro.make_vectorline,
  1677. "20": ApertureMacro.make_vectorline,
  1678. "21": ApertureMacro.make_centerline,
  1679. "22": ApertureMacro.make_lowerleftline,
  1680. "4": ApertureMacro.make_outline,
  1681. "5": ApertureMacro.make_polygon,
  1682. "6": ApertureMacro.make_moire,
  1683. "7": ApertureMacro.make_thermal
  1684. }
  1685. # ## Store modifiers as local variables
  1686. modifiers = modifiers or []
  1687. modifiers = [float(m) for m in modifiers]
  1688. self.locvars = {}
  1689. for i in range(0, len(modifiers)):
  1690. self.locvars[str(i + 1)] = modifiers[i]
  1691. # ## Parse
  1692. self.primitives = [] # Cleanup
  1693. self.geometry = Polygon()
  1694. self.parse_content()
  1695. # ## Make the geometry
  1696. for primitive in self.primitives:
  1697. # Make the primitive
  1698. prim_geo = makers[str(int(primitive[0]))](primitive[1:])
  1699. # Add it (according to polarity)
  1700. # if self.geometry is None and prim_geo['pol'] == 1:
  1701. # self.geometry = prim_geo['geometry']
  1702. # continue
  1703. if prim_geo['pol'] == 1:
  1704. self.geometry = self.geometry.union(prim_geo['geometry'])
  1705. continue
  1706. if prim_geo['pol'] == 0:
  1707. self.geometry = self.geometry.difference(prim_geo['geometry'])
  1708. continue
  1709. return self.geometry
  1710. class AttrDict(dict):
  1711. def __init__(self, *args, **kwargs):
  1712. super(AttrDict, self).__init__(*args, **kwargs)
  1713. self.__dict__ = self
  1714. class CNCjob(Geometry):
  1715. """
  1716. Represents work to be done by a CNC machine.
  1717. *ATTRIBUTES*
  1718. * ``gcode_parsed`` (list): Each is a dictionary:
  1719. ===================== =========================================
  1720. Key Value
  1721. ===================== =========================================
  1722. geom (Shapely.LineString) Tool path (XY plane)
  1723. kind (string) "AB", A is "T" (travel) or
  1724. "C" (cut). B is "F" (fast) or "S" (slow).
  1725. ===================== =========================================
  1726. """
  1727. defaults = {
  1728. "global_zdownrate": None,
  1729. "pp_geometry_name":'default',
  1730. "pp_excellon_name":'default',
  1731. "excellon_optimization_type": "B",
  1732. }
  1733. def __init__(self,
  1734. units="in", kind="generic", tooldia=0.0,
  1735. z_cut=-0.002, z_move=0.1,
  1736. feedrate=3.0, feedrate_z=3.0, feedrate_rapid=3.0, feedrate_probe=3.0,
  1737. pp_geometry_name='default', pp_excellon_name='default',
  1738. depthpercut=0.1,z_pdepth=-0.02,
  1739. spindlespeed=None, spindledir='CW', dwell=True, dwelltime=1000,
  1740. toolchangez=0.787402, toolchange_xy=[0.0, 0.0],
  1741. endz=2.0,
  1742. segx=None,
  1743. segy=None,
  1744. steps_per_circle=None):
  1745. # Used when parsing G-code arcs
  1746. self.steps_per_circle = int(self.app.defaults['cncjob_steps_per_circle'])
  1747. Geometry.__init__(self, geo_steps_per_circle=self.steps_per_circle)
  1748. self.kind = kind
  1749. self.origin_kind = None
  1750. self.units = units
  1751. self.z_cut = z_cut
  1752. self.tool_offset = {}
  1753. self.z_move = z_move
  1754. self.feedrate = feedrate
  1755. self.z_feedrate = feedrate_z
  1756. self.feedrate_rapid = feedrate_rapid
  1757. self.tooldia = tooldia
  1758. self.z_toolchange = toolchangez
  1759. self.xy_toolchange = toolchange_xy
  1760. self.toolchange_xy_type = None
  1761. self.toolC = tooldia
  1762. self.z_end = endz
  1763. self.z_depthpercut = depthpercut
  1764. self.unitcode = {"IN": "G20", "MM": "G21"}
  1765. self.feedminutecode = "G94"
  1766. # self.absolutecode = "G90"
  1767. # self.incrementalcode = "G91"
  1768. self.coordinates_type = self.app.defaults["cncjob_coords_type"]
  1769. self.gcode = ""
  1770. self.gcode_parsed = None
  1771. self.pp_geometry_name = pp_geometry_name
  1772. self.pp_geometry = self.app.postprocessors[self.pp_geometry_name]
  1773. self.pp_excellon_name = pp_excellon_name
  1774. self.pp_excellon = self.app.postprocessors[self.pp_excellon_name]
  1775. self.pp_solderpaste_name = None
  1776. # Controls if the move from Z_Toolchange to Z_Move is done fast with G0 or normally with G1
  1777. self.f_plunge = None
  1778. # Controls if the move from Z_Cutto Z_Move is done fast with G0 or G1 until zero and then G0 to Z_move
  1779. self.f_retract = None
  1780. # how much depth the probe can probe before error
  1781. self.z_pdepth = z_pdepth if z_pdepth else None
  1782. # the feedrate(speed) with which the probel travel while probing
  1783. self.feedrate_probe = feedrate_probe if feedrate_probe else None
  1784. self.spindlespeed = spindlespeed
  1785. self.spindledir = spindledir
  1786. self.dwell = dwell
  1787. self.dwelltime = dwelltime
  1788. self.segx = float(segx) if segx is not None else 0.0
  1789. self.segy = float(segy) if segy is not None else 0.0
  1790. self.input_geometry_bounds = None
  1791. self.oldx = None
  1792. self.oldy = None
  1793. self.tool = 0.0
  1794. # here store the travelled distance
  1795. self.travel_distance = 0.0
  1796. # here store the routing time
  1797. self.routing_time = 0.0
  1798. # used for creating drill CCode geometry; will be updated in the generate_from_excellon_by_tool()
  1799. self.exc_drills = None
  1800. self.exc_tools = None
  1801. # search for toolchange parameters in the Toolchange Custom Code
  1802. self.re_toolchange_custom = re.compile(r'(%[a-zA-Z0-9\-_]+%)')
  1803. # search for toolchange code: M6
  1804. self.re_toolchange = re.compile(r'^\s*(M6)$')
  1805. # Attributes to be included in serialization
  1806. # Always append to it because it carries contents
  1807. # from Geometry.
  1808. self.ser_attrs += ['kind', 'z_cut', 'z_move', 'z_toolchange', 'feedrate', 'z_feedrate', 'feedrate_rapid',
  1809. 'tooldia', 'gcode', 'input_geometry_bounds', 'gcode_parsed', 'steps_per_circle',
  1810. 'z_depthpercut', 'spindlespeed', 'dwell', 'dwelltime']
  1811. @property
  1812. def postdata(self):
  1813. return self.__dict__
  1814. def convert_units(self, units):
  1815. log.debug("camlib.CNCJob.convert_units()")
  1816. factor = Geometry.convert_units(self, units)
  1817. self.z_cut = float(self.z_cut) * factor
  1818. self.z_move *= factor
  1819. self.feedrate *= factor
  1820. self.z_feedrate *= factor
  1821. self.feedrate_rapid *= factor
  1822. self.tooldia *= factor
  1823. self.z_toolchange *= factor
  1824. self.z_end *= factor
  1825. self.z_depthpercut = float(self.z_depthpercut) * factor
  1826. return factor
  1827. def doformat(self, fun, **kwargs):
  1828. return self.doformat2(fun, **kwargs) + "\n"
  1829. def doformat2(self, fun, **kwargs):
  1830. attributes = AttrDict()
  1831. attributes.update(self.postdata)
  1832. attributes.update(kwargs)
  1833. try:
  1834. returnvalue = fun(attributes)
  1835. return returnvalue
  1836. except Exception as e:
  1837. self.app.log.error('Exception occurred within a postprocessor: ' + traceback.format_exc())
  1838. return ''
  1839. def parse_custom_toolchange_code(self, data):
  1840. text = data
  1841. match_list = self.re_toolchange_custom.findall(text)
  1842. if match_list:
  1843. for match in match_list:
  1844. command = match.strip('%')
  1845. try:
  1846. value = getattr(self, command)
  1847. except AttributeError:
  1848. self.app.inform.emit('[ERROR] %s: %s' %
  1849. (_("There is no such parameter"), str(match)))
  1850. log.debug("CNCJob.parse_custom_toolchange_code() --> AttributeError ")
  1851. return 'fail'
  1852. text = text.replace(match, str(value))
  1853. return text
  1854. def optimized_travelling_salesman(self, points, start=None):
  1855. """
  1856. As solving the problem in the brute force way is too slow,
  1857. this function implements a simple heuristic: always
  1858. go to the nearest city.
  1859. Even if this algorithm is extremely simple, it works pretty well
  1860. giving a solution only about 25%% longer than the optimal one (cit. Wikipedia),
  1861. and runs very fast in O(N^2) time complexity.
  1862. >>> optimized_travelling_salesman([[i,j] for i in range(5) for j in range(5)])
  1863. [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 4], [1, 3], [1, 2], [1, 1], [1, 0], [2, 0], [2, 1], [2, 2],
  1864. [2, 3], [2, 4], [3, 4], [3, 3], [3, 2], [3, 1], [3, 0], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4]]
  1865. >>> optimized_travelling_salesman([[0,0],[10,0],[6,0]])
  1866. [[0, 0], [6, 0], [10, 0]]
  1867. """
  1868. if start is None:
  1869. start = points[0]
  1870. must_visit = points
  1871. path = [start]
  1872. # must_visit.remove(start)
  1873. while must_visit:
  1874. nearest = min(must_visit, key=lambda x: distance(path[-1], x))
  1875. path.append(nearest)
  1876. must_visit.remove(nearest)
  1877. return path
  1878. def generate_from_excellon_by_tool(
  1879. self, exobj, tools="all", drillz = 3.0,
  1880. toolchange=False, toolchangez=0.1, toolchangexy='',
  1881. endz=2.0, startz=None,
  1882. excellon_optimization_type='B'):
  1883. """
  1884. Creates gcode for this object from an Excellon object
  1885. for the specified tools.
  1886. :param exobj: Excellon object to process
  1887. :type exobj: Excellon
  1888. :param tools: Comma separated tool names
  1889. :type: tools: str
  1890. :param drillz: drill Z depth
  1891. :type drillz: float
  1892. :param toolchange: Use tool change sequence between tools.
  1893. :type toolchange: bool
  1894. :param toolchangez: Height at which to perform the tool change.
  1895. :type toolchangez: float
  1896. :param toolchangexy: Toolchange X,Y position
  1897. :type toolchangexy: String containing 2 floats separated by comma
  1898. :param startz: Z position just before starting the job
  1899. :type startz: float
  1900. :param endz: final Z position to move to at the end of the CNC job
  1901. :type endz: float
  1902. :param excellon_optimization_type: Single character that defines which drill re-ordering optimisation algorithm
  1903. is to be used: 'M' for meta-heuristic and 'B' for basic
  1904. :type excellon_optimization_type: string
  1905. :return: None
  1906. :rtype: None
  1907. """
  1908. # create a local copy of the exobj.drills so it can be used for creating drill CCode geometry
  1909. self.exc_drills = deepcopy(exobj.drills)
  1910. self.exc_tools = deepcopy(exobj.tools)
  1911. if drillz > 0:
  1912. self.app.inform.emit('[WARNING] %s' %
  1913. _("The Cut Z parameter has positive value. "
  1914. "It is the depth value to drill into material.\n"
  1915. "The Cut Z parameter needs to have a negative value, assuming it is a typo "
  1916. "therefore the app will convert the value to negative. "
  1917. "Check the resulting CNC code (Gcode etc)."))
  1918. self.z_cut = -drillz
  1919. elif drillz == 0:
  1920. self.app.inform.emit('[WARNING] %s: %s' %
  1921. (_("The Cut Z parameter is zero. There will be no cut, skipping file"),
  1922. exobj.options['name']))
  1923. return 'fail'
  1924. else:
  1925. self.z_cut = drillz
  1926. self.z_toolchange = toolchangez
  1927. try:
  1928. if toolchangexy == '':
  1929. self.xy_toolchange = None
  1930. else:
  1931. self.xy_toolchange = [float(eval(a)) for a in toolchangexy.split(",")]
  1932. if len(self.xy_toolchange) < 2:
  1933. self.app.inform.emit('[ERROR]%s' %
  1934. _("The Toolchange X,Y field in Edit -> Preferences has to be "
  1935. "in the format (x, y) \nbut now there is only one value, not two. "))
  1936. return 'fail'
  1937. except Exception as e:
  1938. log.debug("camlib.CNCJob.generate_from_excellon_by_tool() --> %s" % str(e))
  1939. pass
  1940. self.startz = startz
  1941. self.z_end = endz
  1942. self.pp_excellon = self.app.postprocessors[self.pp_excellon_name]
  1943. p = self.pp_excellon
  1944. log.debug("Creating CNC Job from Excellon...")
  1945. # Tools
  1946. # sort the tools list by the second item in tuple (here we have a dict with diameter of the tool)
  1947. # so we actually are sorting the tools by diameter
  1948. #sorted_tools = sorted(exobj.tools.items(), key=lambda t1: t1['C'])
  1949. sort = []
  1950. for k, v in list(exobj.tools.items()):
  1951. sort.append((k, v.get('C')))
  1952. sorted_tools = sorted(sort,key=lambda t1: t1[1])
  1953. if tools == "all":
  1954. tools = [i[0] for i in sorted_tools] # we get a array of ordered tools
  1955. log.debug("Tools 'all' and sorted are: %s" % str(tools))
  1956. else:
  1957. selected_tools = [x.strip() for x in tools.split(",")] # we strip spaces and also separate the tools by ','
  1958. selected_tools = [t1 for t1 in selected_tools if t1 in selected_tools]
  1959. # Create a sorted list of selected tools from the sorted_tools list
  1960. tools = [i for i, j in sorted_tools for k in selected_tools if i == k]
  1961. log.debug("Tools selected and sorted are: %s" % str(tools))
  1962. self.app.inform.emit(_("Creating a list of points to drill..."))
  1963. # Points (Group by tool)
  1964. points = {}
  1965. for drill in exobj.drills:
  1966. if self.app.abort_flag:
  1967. # graceful abort requested by the user
  1968. raise FlatCAMApp.GracefulException
  1969. if drill['tool'] in tools:
  1970. try:
  1971. points[drill['tool']].append(drill['point'])
  1972. except KeyError:
  1973. points[drill['tool']] = [drill['point']]
  1974. #log.debug("Found %d drills." % len(points))
  1975. self.gcode = []
  1976. self.f_plunge = self.app.defaults["excellon_f_plunge"]
  1977. self.f_retract = self.app.defaults["excellon_f_retract"]
  1978. # Initialization
  1979. gcode = self.doformat(p.start_code)
  1980. gcode += self.doformat(p.feedrate_code)
  1981. if toolchange is False:
  1982. if self.xy_toolchange is not None:
  1983. gcode += self.doformat(p.lift_code, x=self.xy_toolchange[0], y=self.xy_toolchange[1])
  1984. gcode += self.doformat(p.startz_code, x=self.xy_toolchange[0], y=self.xy_toolchange[1])
  1985. else:
  1986. gcode += self.doformat(p.lift_code, x=0.0, y=0.0)
  1987. gcode += self.doformat(p.startz_code, x=0.0, y=0.0)
  1988. # Distance callback
  1989. class CreateDistanceCallback(object):
  1990. """Create callback to calculate distances between points."""
  1991. def __init__(self):
  1992. """Initialize distance array."""
  1993. locations = create_data_array()
  1994. size = len(locations)
  1995. self.matrix = {}
  1996. for from_node in range(size):
  1997. self.matrix[from_node] = {}
  1998. for to_node in range(size):
  1999. if from_node == to_node:
  2000. self.matrix[from_node][to_node] = 0
  2001. else:
  2002. x1 = locations[from_node][0]
  2003. y1 = locations[from_node][1]
  2004. x2 = locations[to_node][0]
  2005. y2 = locations[to_node][1]
  2006. self.matrix[from_node][to_node] = distance_euclidian(x1, y1, x2, y2)
  2007. # def Distance(self, from_node, to_node):
  2008. # return int(self.matrix[from_node][to_node])
  2009. def Distance(self, from_index, to_index):
  2010. # Convert from routing variable Index to distance matrix NodeIndex.
  2011. from_node = manager.IndexToNode(from_index)
  2012. to_node = manager.IndexToNode(to_index)
  2013. return self.matrix[from_node][to_node]
  2014. # Create the data.
  2015. def create_data_array():
  2016. locations = []
  2017. for point in points[tool]:
  2018. locations.append((point.coords.xy[0][0], point.coords.xy[1][0]))
  2019. return locations
  2020. if self.xy_toolchange is not None:
  2021. self.oldx = self.xy_toolchange[0]
  2022. self.oldy = self.xy_toolchange[1]
  2023. else:
  2024. self.oldx = 0.0
  2025. self.oldy = 0.0
  2026. measured_distance = 0.0
  2027. measured_down_distance = 0.0
  2028. measured_up_to_zero_distance = 0.0
  2029. measured_lift_distance = 0.0
  2030. self.app.inform.emit('%s...' %
  2031. _("Starting G-Code"))
  2032. current_platform = platform.architecture()[0]
  2033. if current_platform == '64bit':
  2034. used_excellon_optimization_type = excellon_optimization_type
  2035. if used_excellon_optimization_type == 'M':
  2036. log.debug("Using OR-Tools Metaheuristic Guided Local Search drill path optimization.")
  2037. if exobj.drills:
  2038. for tool in tools:
  2039. self.tool=tool
  2040. self.postdata['toolC'] = exobj.tools[tool]["C"]
  2041. self.tooldia = exobj.tools[tool]["C"]
  2042. if self.app.abort_flag:
  2043. # graceful abort requested by the user
  2044. raise FlatCAMApp.GracefulException
  2045. # ###############################################
  2046. # ############ Create the data. #################
  2047. # ###############################################
  2048. node_list = []
  2049. locations = create_data_array()
  2050. tsp_size = len(locations)
  2051. num_routes = 1 # The number of routes, which is 1 in the TSP.
  2052. # Nodes are indexed from 0 to tsp_size - 1. The depot is the starting node of the route.
  2053. depot = 0
  2054. # Create routing model.
  2055. if tsp_size > 0:
  2056. manager = pywrapcp.RoutingIndexManager(tsp_size, num_routes, depot)
  2057. routing = pywrapcp.RoutingModel(manager)
  2058. search_parameters = pywrapcp.DefaultRoutingSearchParameters()
  2059. search_parameters.local_search_metaheuristic = (
  2060. routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
  2061. # Set search time limit in milliseconds.
  2062. if float(self.app.defaults["excellon_search_time"]) != 0:
  2063. search_parameters.time_limit.seconds = int(
  2064. float(self.app.defaults["excellon_search_time"]))
  2065. else:
  2066. search_parameters.time_limit.seconds = 3
  2067. # Callback to the distance function. The callback takes two
  2068. # arguments (the from and to node indices) and returns the distance between them.
  2069. dist_between_locations = CreateDistanceCallback()
  2070. dist_callback = dist_between_locations.Distance
  2071. transit_callback_index = routing.RegisterTransitCallback(dist_callback)
  2072. routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
  2073. # Solve, returns a solution if any.
  2074. assignment = routing.SolveWithParameters(search_parameters)
  2075. if assignment:
  2076. # Solution cost.
  2077. log.info("Total distance: " + str(assignment.ObjectiveValue()))
  2078. # Inspect solution.
  2079. # Only one route here; otherwise iterate from 0 to routing.vehicles() - 1.
  2080. route_number = 0
  2081. node = routing.Start(route_number)
  2082. start_node = node
  2083. while not routing.IsEnd(node):
  2084. if self.app.abort_flag:
  2085. # graceful abort requested by the user
  2086. raise FlatCAMApp.GracefulException
  2087. node_list.append(node)
  2088. node = assignment.Value(routing.NextVar(node))
  2089. else:
  2090. log.warning('No solution found.')
  2091. else:
  2092. log.warning('Specify an instance greater than 0.')
  2093. # ############################################# ##
  2094. # Only if tool has points.
  2095. if tool in points:
  2096. if self.app.abort_flag:
  2097. # graceful abort requested by the user
  2098. raise FlatCAMApp.GracefulException
  2099. # Tool change sequence (optional)
  2100. if toolchange:
  2101. gcode += self.doformat(p.toolchange_code,toolchangexy=(self.oldx, self.oldy))
  2102. gcode += self.doformat(p.spindle_code) # Spindle start
  2103. if self.dwell is True:
  2104. gcode += self.doformat(p.dwell_code) # Dwell time
  2105. else:
  2106. gcode += self.doformat(p.spindle_code)
  2107. if self.dwell is True:
  2108. gcode += self.doformat(p.dwell_code) # Dwell time
  2109. if self.units == 'MM':
  2110. current_tooldia = float('%.2f' % float(exobj.tools[tool]["C"]))
  2111. else:
  2112. current_tooldia = float('%.4f' % float(exobj.tools[tool]["C"]))
  2113. self.app.inform.emit(
  2114. '%s: %s%s.' % (_("Starting G-Code for tool with diameter"),
  2115. str(current_tooldia),
  2116. str(self.units))
  2117. )
  2118. # TODO apply offset only when using the GUI, for TclCommand this will create an error
  2119. # because the values for Z offset are created in build_ui()
  2120. try:
  2121. z_offset = float(self.tool_offset[current_tooldia]) * (-1)
  2122. except KeyError:
  2123. z_offset = 0
  2124. self.z_cut += z_offset
  2125. self.coordinates_type = self.app.defaults["cncjob_coords_type"]
  2126. if self.coordinates_type == "G90":
  2127. # Drillling! for Absolute coordinates type G90
  2128. # variables to display the percentage of work done
  2129. geo_len = len(node_list)
  2130. disp_number = 0
  2131. old_disp_number = 0
  2132. log.warning("Number of drills for which to generate GCode: %s" % str(geo_len))
  2133. loc_nr = 0
  2134. for k in node_list:
  2135. if self.app.abort_flag:
  2136. # graceful abort requested by the user
  2137. raise FlatCAMApp.GracefulException
  2138. locx = locations[k][0]
  2139. locy = locations[k][1]
  2140. gcode += self.doformat(p.rapid_code, x=locx, y=locy)
  2141. gcode += self.doformat(p.down_code, x=locx, y=locy)
  2142. measured_down_distance += abs(self.z_cut) + abs(self.z_move)
  2143. if self.f_retract is False:
  2144. gcode += self.doformat(p.up_to_zero_code, x=locx, y=locy)
  2145. measured_up_to_zero_distance += abs(self.z_cut)
  2146. measured_lift_distance += abs(self.z_move)
  2147. else:
  2148. measured_lift_distance += abs(self.z_cut) + abs(self.z_move)
  2149. gcode += self.doformat(p.lift_code, x=locx, y=locy)
  2150. measured_distance += abs(distance_euclidian(locx, locy, self.oldx, self.oldy))
  2151. self.oldx = locx
  2152. self.oldy = locy
  2153. loc_nr += 1
  2154. disp_number = int(np.interp(loc_nr, [0, geo_len], [0, 100]))
  2155. if old_disp_number < disp_number <= 100:
  2156. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  2157. old_disp_number = disp_number
  2158. else:
  2159. self.app.inform.emit('[ERROR_NOTCL] %s...' %
  2160. _('G91 coordinates not implemented'))
  2161. return 'fail'
  2162. else:
  2163. log.debug("camlib.CNCJob.generate_from_excellon_by_tool() --> "
  2164. "The loaded Excellon file has no drills ...")
  2165. self.app.inform.emit('[ERROR_NOTCL] %s...' %
  2166. _('The loaded Excellon file has no drills'))
  2167. return 'fail'
  2168. log.debug("The total travel distance with OR-TOOLS Metaheuristics is: %s" % str(measured_distance))
  2169. if used_excellon_optimization_type == 'B':
  2170. log.debug("Using OR-Tools Basic drill path optimization.")
  2171. if exobj.drills:
  2172. for tool in tools:
  2173. if self.app.abort_flag:
  2174. # graceful abort requested by the user
  2175. raise FlatCAMApp.GracefulException
  2176. self.tool=tool
  2177. self.postdata['toolC']=exobj.tools[tool]["C"]
  2178. self.tooldia = exobj.tools[tool]["C"]
  2179. # ############################################# ##
  2180. node_list = []
  2181. locations = create_data_array()
  2182. tsp_size = len(locations)
  2183. num_routes = 1 # The number of routes, which is 1 in the TSP.
  2184. # Nodes are indexed from 0 to tsp_size - 1. The depot is the starting node of the route.
  2185. depot = 0
  2186. # Create routing model.
  2187. if tsp_size > 0:
  2188. manager = pywrapcp.RoutingIndexManager(tsp_size, num_routes, depot)
  2189. routing = pywrapcp.RoutingModel(manager)
  2190. search_parameters = pywrapcp.DefaultRoutingSearchParameters()
  2191. # Callback to the distance function. The callback takes two
  2192. # arguments (the from and to node indices) and returns the distance between them.
  2193. dist_between_locations = CreateDistanceCallback()
  2194. dist_callback = dist_between_locations.Distance
  2195. transit_callback_index = routing.RegisterTransitCallback(dist_callback)
  2196. routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
  2197. # Solve, returns a solution if any.
  2198. assignment = routing.SolveWithParameters(search_parameters)
  2199. if assignment:
  2200. # Solution cost.
  2201. log.info("Total distance: " + str(assignment.ObjectiveValue()))
  2202. # Inspect solution.
  2203. # Only one route here; otherwise iterate from 0 to routing.vehicles() - 1.
  2204. route_number = 0
  2205. node = routing.Start(route_number)
  2206. start_node = node
  2207. while not routing.IsEnd(node):
  2208. node_list.append(node)
  2209. node = assignment.Value(routing.NextVar(node))
  2210. else:
  2211. log.warning('No solution found.')
  2212. else:
  2213. log.warning('Specify an instance greater than 0.')
  2214. # ############################################# ##
  2215. # Only if tool has points.
  2216. if tool in points:
  2217. if self.app.abort_flag:
  2218. # graceful abort requested by the user
  2219. raise FlatCAMApp.GracefulException
  2220. # Tool change sequence (optional)
  2221. if toolchange:
  2222. gcode += self.doformat(p.toolchange_code,toolchangexy=(self.oldx, self.oldy))
  2223. gcode += self.doformat(p.spindle_code) # Spindle start)
  2224. if self.dwell is True:
  2225. gcode += self.doformat(p.dwell_code) # Dwell time
  2226. else:
  2227. gcode += self.doformat(p.spindle_code)
  2228. if self.dwell is True:
  2229. gcode += self.doformat(p.dwell_code) # Dwell time
  2230. if self.units == 'MM':
  2231. current_tooldia = float('%.2f' % float(exobj.tools[tool]["C"]))
  2232. else:
  2233. current_tooldia = float('%.4f' % float(exobj.tools[tool]["C"]))
  2234. self.app.inform.emit(
  2235. '%s: %s%s.' % (_("Starting G-Code for tool with diameter"),
  2236. str(current_tooldia),
  2237. str(self.units))
  2238. )
  2239. # TODO apply offset only when using the GUI, for TclCommand this will create an error
  2240. # because the values for Z offset are created in build_ui()
  2241. try:
  2242. z_offset = float(self.tool_offset[current_tooldia]) * (-1)
  2243. except KeyError:
  2244. z_offset = 0
  2245. self.z_cut += z_offset
  2246. self.coordinates_type = self.app.defaults["cncjob_coords_type"]
  2247. if self.coordinates_type == "G90":
  2248. # Drillling! for Absolute coordinates type G90
  2249. # variables to display the percentage of work done
  2250. geo_len = len(node_list)
  2251. disp_number = 0
  2252. old_disp_number = 0
  2253. log.warning("Number of drills for which to generate GCode: %s" % str(geo_len))
  2254. loc_nr = 0
  2255. for k in node_list:
  2256. if self.app.abort_flag:
  2257. # graceful abort requested by the user
  2258. raise FlatCAMApp.GracefulException
  2259. locx = locations[k][0]
  2260. locy = locations[k][1]
  2261. gcode += self.doformat(p.rapid_code, x=locx, y=locy)
  2262. gcode += self.doformat(p.down_code, x=locx, y=locy)
  2263. measured_down_distance += abs(self.z_cut) + abs(self.z_move)
  2264. if self.f_retract is False:
  2265. gcode += self.doformat(p.up_to_zero_code, x=locx, y=locy)
  2266. measured_up_to_zero_distance += abs(self.z_cut)
  2267. measured_lift_distance += abs(self.z_move)
  2268. else:
  2269. measured_lift_distance += abs(self.z_cut) + abs(self.z_move)
  2270. gcode += self.doformat(p.lift_code, x=locx, y=locy)
  2271. measured_distance += abs(distance_euclidian(locx, locy, self.oldx, self.oldy))
  2272. self.oldx = locx
  2273. self.oldy = locy
  2274. loc_nr += 1
  2275. disp_number = int(np.interp(loc_nr, [0, geo_len], [0, 100]))
  2276. if old_disp_number < disp_number <= 100:
  2277. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  2278. old_disp_number = disp_number
  2279. else:
  2280. self.app.inform.emit('[ERROR_NOTCL] %s...' %
  2281. _('G91 coordinates not implemented'))
  2282. return 'fail'
  2283. else:
  2284. log.debug("camlib.CNCJob.generate_from_excellon_by_tool() --> "
  2285. "The loaded Excellon file has no drills ...")
  2286. self.app.inform.emit('[ERROR_NOTCL] %s...' %
  2287. _('The loaded Excellon file has no drills'))
  2288. return 'fail'
  2289. log.debug("The total travel distance with OR-TOOLS Basic Algorithm is: %s" % str(measured_distance))
  2290. else:
  2291. used_excellon_optimization_type = 'T'
  2292. if used_excellon_optimization_type == 'T':
  2293. log.debug("Using Travelling Salesman drill path optimization.")
  2294. for tool in tools:
  2295. if self.app.abort_flag:
  2296. # graceful abort requested by the user
  2297. raise FlatCAMApp.GracefulException
  2298. if exobj.drills:
  2299. self.tool = tool
  2300. self.postdata['toolC'] = exobj.tools[tool]["C"]
  2301. self.tooldia = exobj.tools[tool]["C"]
  2302. # Only if tool has points.
  2303. if tool in points:
  2304. if self.app.abort_flag:
  2305. # graceful abort requested by the user
  2306. raise FlatCAMApp.GracefulException
  2307. # Tool change sequence (optional)
  2308. if toolchange:
  2309. gcode += self.doformat(p.toolchange_code, toolchangexy=(self.oldx, self.oldy))
  2310. gcode += self.doformat(p.spindle_code) # Spindle start)
  2311. if self.dwell is True:
  2312. gcode += self.doformat(p.dwell_code) # Dwell time
  2313. else:
  2314. gcode += self.doformat(p.spindle_code)
  2315. if self.dwell is True:
  2316. gcode += self.doformat(p.dwell_code) # Dwell time
  2317. if self.units == 'MM':
  2318. current_tooldia = float('%.2f' % float(exobj.tools[tool]["C"]))
  2319. else:
  2320. current_tooldia = float('%.4f' % float(exobj.tools[tool]["C"]))
  2321. self.app.inform.emit(
  2322. '%s: %s%s.' % (_("Starting G-Code for tool with diameter"),
  2323. str(current_tooldia),
  2324. str(self.units))
  2325. )
  2326. # TODO apply offset only when using the GUI, for TclCommand this will create an error
  2327. # because the values for Z offset are created in build_ui()
  2328. try:
  2329. z_offset = float(self.tool_offset[current_tooldia]) * (-1)
  2330. except KeyError:
  2331. z_offset = 0
  2332. self.z_cut += z_offset
  2333. self.coordinates_type = self.app.defaults["cncjob_coords_type"]
  2334. if self.coordinates_type == "G90":
  2335. # Drillling! for Absolute coordinates type G90
  2336. altPoints = []
  2337. for point in points[tool]:
  2338. altPoints.append((point.coords.xy[0][0], point.coords.xy[1][0]))
  2339. node_list = self.optimized_travelling_salesman(altPoints)
  2340. # variables to display the percentage of work done
  2341. geo_len = len(node_list)
  2342. disp_number = 0
  2343. old_disp_number = 0
  2344. log.warning("Number of drills for which to generate GCode: %s" % str(geo_len))
  2345. loc_nr = 0
  2346. for point in node_list:
  2347. if self.app.abort_flag:
  2348. # graceful abort requested by the user
  2349. raise FlatCAMApp.GracefulException
  2350. gcode += self.doformat(p.rapid_code, x=point[0], y=point[1])
  2351. gcode += self.doformat(p.down_code, x=point[0], y=point[1])
  2352. measured_down_distance += abs(self.z_cut) + abs(self.z_move)
  2353. if self.f_retract is False:
  2354. gcode += self.doformat(p.up_to_zero_code, x=point[0], y=point[1])
  2355. measured_up_to_zero_distance += abs(self.z_cut)
  2356. measured_lift_distance += abs(self.z_move)
  2357. else:
  2358. measured_lift_distance += abs(self.z_cut) + abs(self.z_move)
  2359. gcode += self.doformat(p.lift_code, x=point[0], y=point[1])
  2360. measured_distance += abs(distance_euclidian(point[0], point[1], self.oldx, self.oldy))
  2361. self.oldx = point[0]
  2362. self.oldy = point[1]
  2363. loc_nr += 1
  2364. disp_number = int(np.interp(loc_nr, [0, geo_len], [0, 100]))
  2365. if old_disp_number < disp_number <= 100:
  2366. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  2367. old_disp_number = disp_number
  2368. else:
  2369. self.app.inform.emit('[ERROR_NOTCL] %s...' %
  2370. _('G91 coordinates not implemented'))
  2371. return 'fail'
  2372. else:
  2373. log.debug("camlib.CNCJob.generate_from_excellon_by_tool() --> "
  2374. "The loaded Excellon file has no drills ...")
  2375. self.app.inform.emit('[ERROR_NOTCL] %s...' %
  2376. _('The loaded Excellon file has no drills'))
  2377. return 'fail'
  2378. log.debug("The total travel distance with Travelling Salesman Algorithm is: %s" % str(measured_distance))
  2379. gcode += self.doformat(p.spindle_stop_code) # Spindle stop
  2380. gcode += self.doformat(p.end_code, x=0, y=0)
  2381. measured_distance += abs(distance_euclidian(self.oldx, self.oldy, 0, 0))
  2382. log.debug("The total travel distance including travel to end position is: %s" %
  2383. str(measured_distance) + '\n')
  2384. self.travel_distance = measured_distance
  2385. # I use the value of self.feedrate_rapid for the feadrate in case of the measure_lift_distance and for
  2386. # traveled_time because it is not always possible to determine the feedrate that the CNC machine uses
  2387. # for G0 move (the fastest speed available to the CNC router). Although self.feedrate_rapids is used only with
  2388. # Marlin postprocessor and derivatives.
  2389. self.routing_time = (measured_down_distance + measured_up_to_zero_distance) / self.feedrate
  2390. lift_time = measured_lift_distance / self.feedrate_rapid
  2391. traveled_time = measured_distance / self.feedrate_rapid
  2392. self.routing_time += lift_time + traveled_time
  2393. self.gcode = gcode
  2394. self.app.inform.emit(_("Finished G-Code generation..."))
  2395. return 'OK'
  2396. def generate_from_multitool_geometry(self, geometry, append=True,
  2397. tooldia=None, offset=0.0, tolerance=0, z_cut=1.0, z_move=2.0,
  2398. feedrate=2.0, feedrate_z=2.0, feedrate_rapid=30,
  2399. spindlespeed=None, spindledir='CW', dwell=False, dwelltime=1.0,
  2400. multidepth=False, depthpercut=None,
  2401. toolchange=False, toolchangez=1.0, toolchangexy="0.0, 0.0", extracut=False,
  2402. startz=None, endz=2.0, pp_geometry_name=None, tool_no=1):
  2403. """
  2404. Algorithm to generate from multitool Geometry.
  2405. Algorithm description:
  2406. ----------------------
  2407. Uses RTree to find the nearest path to follow.
  2408. :param geometry:
  2409. :param append:
  2410. :param tooldia:
  2411. :param tolerance:
  2412. :param multidepth: If True, use multiple passes to reach
  2413. the desired depth.
  2414. :param depthpercut: Maximum depth in each pass.
  2415. :param extracut: Adds (or not) an extra cut at the end of each path
  2416. overlapping the first point in path to ensure complete copper removal
  2417. :return: GCode - string
  2418. """
  2419. log.debug("Generate_from_multitool_geometry()")
  2420. temp_solid_geometry = []
  2421. if offset != 0.0:
  2422. for it in geometry:
  2423. # if the geometry is a closed shape then create a Polygon out of it
  2424. if isinstance(it, LineString):
  2425. c = it.coords
  2426. if c[0] == c[-1]:
  2427. it = Polygon(it)
  2428. temp_solid_geometry.append(it.buffer(offset, join_style=2))
  2429. else:
  2430. temp_solid_geometry = geometry
  2431. # ## Flatten the geometry. Only linear elements (no polygons) remain.
  2432. flat_geometry = self.flatten(temp_solid_geometry, pathonly=True)
  2433. log.debug("%d paths" % len(flat_geometry))
  2434. self.tooldia = float(tooldia) if tooldia else None
  2435. self.z_cut = float(z_cut) if z_cut else None
  2436. self.z_move = float(z_move) if z_move else None
  2437. self.feedrate = float(feedrate) if feedrate else None
  2438. self.z_feedrate = float(feedrate_z) if feedrate_z else None
  2439. self.feedrate_rapid = float(feedrate_rapid) if feedrate_rapid else None
  2440. self.spindlespeed = int(spindlespeed) if spindlespeed else None
  2441. self.spindledir = spindledir
  2442. self.dwell = dwell
  2443. self.dwelltime = float(dwelltime) if dwelltime else None
  2444. self.startz = float(startz) if startz else None
  2445. self.z_end = float(endz) if endz else None
  2446. self.z_depthpercut = float(depthpercut) if depthpercut else None
  2447. self.multidepth = multidepth
  2448. self.z_toolchange = float(toolchangez) if toolchangez else None
  2449. # it servers in the postprocessor file
  2450. self.tool = tool_no
  2451. try:
  2452. if toolchangexy == '':
  2453. self.xy_toolchange = None
  2454. else:
  2455. self.xy_toolchange = [float(eval(a)) for a in toolchangexy.split(",")]
  2456. if len(self.xy_toolchange) < 2:
  2457. self.app.inform.emit('[ERROR] %s' % _("The Toolchange X,Y field in Edit -> Preferences has to be "
  2458. "in the format (x, y) \n"
  2459. "but now there is only one value, not two."))
  2460. return 'fail'
  2461. except Exception as e:
  2462. log.debug("camlib.CNCJob.generate_from_multitool_geometry() --> %s" % str(e))
  2463. pass
  2464. self.pp_geometry_name = pp_geometry_name if pp_geometry_name else 'default'
  2465. self.f_plunge = self.app.defaults["geometry_f_plunge"]
  2466. if self.z_cut is None:
  2467. self.app.inform.emit('[ERROR_NOTCL] %s' %
  2468. _("Cut_Z parameter is None or zero. Most likely a bad combinations of "
  2469. "other parameters."))
  2470. return 'fail'
  2471. if self.z_cut > 0:
  2472. self.app.inform.emit('[WARNING] %s' %
  2473. _("The Cut Z parameter has positive value. "
  2474. "It is the depth value to cut into material.\n"
  2475. "The Cut Z parameter needs to have a negative value, assuming it is a typo "
  2476. "therefore the app will convert the value to negative."
  2477. "Check the resulting CNC code (Gcode etc)."))
  2478. self.z_cut = -self.z_cut
  2479. elif self.z_cut == 0:
  2480. self.app.inform.emit('[WARNING] %s: %s' %
  2481. (_("The Cut Z parameter is zero. There will be no cut, skipping file"),
  2482. self.options['name']))
  2483. return 'fail'
  2484. # made sure that depth_per_cut is no more then the z_cut
  2485. if abs(self.z_cut) < self.z_depthpercut:
  2486. self.z_depthpercut = abs(self.z_cut)
  2487. if self.z_move is None:
  2488. self.app.inform.emit('[ERROR_NOTCL] %s' %
  2489. _("Travel Z parameter is None or zero."))
  2490. return 'fail'
  2491. if self.z_move < 0:
  2492. self.app.inform.emit('[WARNING] %s' %
  2493. _("The Travel Z parameter has negative value. "
  2494. "It is the height value to travel between cuts.\n"
  2495. "The Z Travel parameter needs to have a positive value, assuming it is a typo "
  2496. "therefore the app will convert the value to positive."
  2497. "Check the resulting CNC code (Gcode etc)."))
  2498. self.z_move = -self.z_move
  2499. elif self.z_move == 0:
  2500. self.app.inform.emit('[WARNING] %s: %s' %
  2501. (_("The Z Travel parameter is zero. This is dangerous, skipping file"),
  2502. self.options['name']))
  2503. return 'fail'
  2504. # ## Index first and last points in paths
  2505. # What points to index.
  2506. def get_pts(o):
  2507. return [o.coords[0], o.coords[-1]]
  2508. # Create the indexed storage.
  2509. storage = FlatCAMRTreeStorage()
  2510. storage.get_points = get_pts
  2511. # Store the geometry
  2512. log.debug("Indexing geometry before generating G-Code...")
  2513. self.app.inform.emit(_("Indexing geometry before generating G-Code..."))
  2514. for shape in flat_geometry:
  2515. if self.app.abort_flag:
  2516. # graceful abort requested by the user
  2517. raise FlatCAMApp.GracefulException
  2518. if shape is not None: # TODO: This shouldn't have happened.
  2519. storage.insert(shape)
  2520. # self.input_geometry_bounds = geometry.bounds()
  2521. if not append:
  2522. self.gcode = ""
  2523. # tell postprocessor the number of tool (for toolchange)
  2524. self.tool = tool_no
  2525. # this is the tool diameter, it is used as such to accommodate the postprocessor who need the tool diameter
  2526. # given under the name 'toolC'
  2527. self.postdata['toolC'] = self.tooldia
  2528. # Initial G-Code
  2529. self.pp_geometry = self.app.postprocessors[self.pp_geometry_name]
  2530. p = self.pp_geometry
  2531. self.gcode = self.doformat(p.start_code)
  2532. self.gcode += self.doformat(p.feedrate_code) # sets the feed rate
  2533. if toolchange is False:
  2534. self.gcode += self.doformat(p.lift_code, x=0, y=0) # Move (up) to travel height
  2535. self.gcode += self.doformat(p.startz_code, x=0, y=0)
  2536. if toolchange:
  2537. # if "line_xyz" in self.pp_geometry_name:
  2538. # self.gcode += self.doformat(p.toolchange_code, x=self.xy_toolchange[0], y=self.xy_toolchange[1])
  2539. # else:
  2540. # self.gcode += self.doformat(p.toolchange_code)
  2541. self.gcode += self.doformat(p.toolchange_code)
  2542. if 'laser' not in self.pp_geometry_name:
  2543. self.gcode += self.doformat(p.spindle_code) # Spindle start
  2544. else:
  2545. # for laser this will disable the laser
  2546. self.gcode += self.doformat(p.lift_code, x=self.oldx, y=self.oldy) # Move (up) to travel height
  2547. if self.dwell is True:
  2548. self.gcode += self.doformat(p.dwell_code) # Dwell time
  2549. else:
  2550. if 'laser' not in self.pp_geometry_name:
  2551. self.gcode += self.doformat(p.spindle_code) # Spindle start
  2552. if self.dwell is True:
  2553. self.gcode += self.doformat(p.dwell_code) # Dwell time
  2554. total_travel = 0.0
  2555. total_cut = 0.0
  2556. # ## Iterate over geometry paths getting the nearest each time.
  2557. log.debug("Starting G-Code...")
  2558. self.app.inform.emit(_("Starting G-Code..."))
  2559. path_count = 0
  2560. current_pt = (0, 0)
  2561. # variables to display the percentage of work done
  2562. geo_len = len(flat_geometry)
  2563. disp_number = 0
  2564. old_disp_number = 0
  2565. log.warning("Number of paths for which to generate GCode: %s" % str(geo_len))
  2566. if self.units == 'MM':
  2567. current_tooldia = float('%.2f' % float(self.tooldia))
  2568. else:
  2569. current_tooldia = float('%.4f' % float(self.tooldia))
  2570. self.app.inform.emit(
  2571. '%s: %s%s.' % (_("Starting G-Code for tool with diameter"),
  2572. str(current_tooldia),
  2573. str(self.units))
  2574. )
  2575. pt, geo = storage.nearest(current_pt)
  2576. try:
  2577. while True:
  2578. if self.app.abort_flag:
  2579. # graceful abort requested by the user
  2580. raise FlatCAMApp.GracefulException
  2581. path_count += 1
  2582. # Remove before modifying, otherwise deletion will fail.
  2583. storage.remove(geo)
  2584. # If last point in geometry is the nearest but prefer the first one if last point == first point
  2585. # then reverse coordinates.
  2586. if pt != geo.coords[0] and pt == geo.coords[-1]:
  2587. geo.coords = list(geo.coords)[::-1]
  2588. # ---------- Single depth/pass --------
  2589. if not multidepth:
  2590. # calculate the cut distance
  2591. total_cut = total_cut + geo.length
  2592. self.gcode += self.create_gcode_single_pass(geo, extracut, tolerance, old_point=current_pt)
  2593. # --------- Multi-pass ---------
  2594. else:
  2595. # calculate the cut distance
  2596. # due of the number of cuts (multi depth) it has to multiplied by the number of cuts
  2597. nr_cuts = 0
  2598. depth = abs(self.z_cut)
  2599. while depth > 0:
  2600. nr_cuts += 1
  2601. depth -= float(self.z_depthpercut)
  2602. total_cut += (geo.length * nr_cuts)
  2603. self.gcode += self.create_gcode_multi_pass(geo, extracut, tolerance,
  2604. postproc=p, old_point=current_pt)
  2605. # calculate the total distance
  2606. total_travel = total_travel + abs(distance(pt1=current_pt, pt2=pt))
  2607. current_pt = geo.coords[-1]
  2608. pt, geo = storage.nearest(current_pt) # Next
  2609. disp_number = int(np.interp(path_count, [0, geo_len], [0, 100]))
  2610. if old_disp_number < disp_number <= 100:
  2611. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  2612. old_disp_number = disp_number
  2613. except StopIteration: # Nothing found in storage.
  2614. pass
  2615. log.debug("Finished G-Code... %s paths traced." % path_count)
  2616. # add move to end position
  2617. total_travel += abs(distance_euclidian(current_pt[0], current_pt[1], 0, 0))
  2618. self.travel_distance += total_travel + total_cut
  2619. self.routing_time += total_cut / self.feedrate
  2620. # Finish
  2621. self.gcode += self.doformat(p.spindle_stop_code)
  2622. self.gcode += self.doformat(p.lift_code, x=current_pt[0], y=current_pt[1])
  2623. self.gcode += self.doformat(p.end_code, x=0, y=0)
  2624. self.app.inform.emit('%s... %s %s.' %
  2625. (_("Finished G-Code generation"),
  2626. str(path_count),
  2627. _("paths traced")
  2628. )
  2629. )
  2630. return self.gcode
  2631. def generate_from_geometry_2(
  2632. self, geometry, append=True,
  2633. tooldia=None, offset=0.0, tolerance=0,
  2634. z_cut=1.0, z_move=2.0,
  2635. feedrate=2.0, feedrate_z=2.0, feedrate_rapid=30,
  2636. spindlespeed=None, spindledir='CW', dwell=False, dwelltime=1.0,
  2637. multidepth=False, depthpercut=None,
  2638. toolchange=False, toolchangez=1.0, toolchangexy="0.0, 0.0",
  2639. extracut=False, startz=None, endz=2.0,
  2640. pp_geometry_name=None, tool_no=1):
  2641. """
  2642. Second algorithm to generate from Geometry.
  2643. Algorithm description:
  2644. ----------------------
  2645. Uses RTree to find the nearest path to follow.
  2646. :param geometry:
  2647. :param append:
  2648. :param tooldia:
  2649. :param tolerance:
  2650. :param multidepth: If True, use multiple passes to reach
  2651. the desired depth.
  2652. :param depthpercut: Maximum depth in each pass.
  2653. :param extracut: Adds (or not) an extra cut at the end of each path
  2654. overlapping the first point in path to ensure complete copper removal
  2655. :return: None
  2656. """
  2657. if not isinstance(geometry, Geometry):
  2658. self.app.inform.emit('[ERROR] %s: %s' %
  2659. (_("Expected a Geometry, got"), type(geometry)))
  2660. return 'fail'
  2661. log.debug("Generate_from_geometry_2()")
  2662. # if solid_geometry is empty raise an exception
  2663. if not geometry.solid_geometry:
  2664. self.app.inform.emit('[ERROR_NOTCL] %s' %
  2665. _("Trying to generate a CNC Job "
  2666. "from a Geometry object without solid_geometry."))
  2667. temp_solid_geometry = []
  2668. def bounds_rec(obj):
  2669. if type(obj) is list:
  2670. minx = Inf
  2671. miny = Inf
  2672. maxx = -Inf
  2673. maxy = -Inf
  2674. for k in obj:
  2675. if type(k) is dict:
  2676. for key in k:
  2677. minx_, miny_, maxx_, maxy_ = bounds_rec(k[key])
  2678. minx = min(minx, minx_)
  2679. miny = min(miny, miny_)
  2680. maxx = max(maxx, maxx_)
  2681. maxy = max(maxy, maxy_)
  2682. else:
  2683. minx_, miny_, maxx_, maxy_ = bounds_rec(k)
  2684. minx = min(minx, minx_)
  2685. miny = min(miny, miny_)
  2686. maxx = max(maxx, maxx_)
  2687. maxy = max(maxy, maxy_)
  2688. return minx, miny, maxx, maxy
  2689. else:
  2690. # it's a Shapely object, return it's bounds
  2691. return obj.bounds
  2692. if offset != 0.0:
  2693. offset_for_use = offset
  2694. if offset < 0:
  2695. a, b, c, d = bounds_rec(geometry.solid_geometry)
  2696. # if the offset is less than half of the total length or less than half of the total width of the
  2697. # solid geometry it's obvious we can't do the offset
  2698. if -offset > ((c - a) / 2) or -offset > ((d - b) / 2):
  2699. self.app.inform.emit('[ERROR_NOTCL] %s' % _(
  2700. "The Tool Offset value is too negative to use "
  2701. "for the current_geometry.\n"
  2702. "Raise the value (in module) and try again."))
  2703. return 'fail'
  2704. # hack: make offset smaller by 0.0000000001 which is insignificant difference but allow the job
  2705. # to continue
  2706. elif -offset == ((c - a) / 2) or -offset == ((d - b) / 2):
  2707. offset_for_use = offset - 0.0000000001
  2708. for it in geometry.solid_geometry:
  2709. # if the geometry is a closed shape then create a Polygon out of it
  2710. if isinstance(it, LineString):
  2711. c = it.coords
  2712. if c[0] == c[-1]:
  2713. it = Polygon(it)
  2714. temp_solid_geometry.append(it.buffer(offset_for_use, join_style=2))
  2715. else:
  2716. temp_solid_geometry = geometry.solid_geometry
  2717. # ## Flatten the geometry. Only linear elements (no polygons) remain.
  2718. flat_geometry = self.flatten(temp_solid_geometry, pathonly=True)
  2719. log.debug("%d paths" % len(flat_geometry))
  2720. try:
  2721. self.tooldia = float(tooldia) if tooldia else None
  2722. except ValueError:
  2723. self.tooldia = [float(el) for el in tooldia.split(',') if el != ''] if tooldia else None
  2724. self.z_cut = float(z_cut) if z_cut else None
  2725. self.z_move = float(z_move) if z_move else None
  2726. self.feedrate = float(feedrate) if feedrate else None
  2727. self.z_feedrate = float(feedrate_z) if feedrate_z else None
  2728. self.feedrate_rapid = float(feedrate_rapid) if feedrate_rapid else None
  2729. self.spindlespeed = int(spindlespeed) if spindlespeed else None
  2730. self.spindledir = spindledir
  2731. self.dwell = dwell
  2732. self.dwelltime = float(dwelltime) if dwelltime else None
  2733. self.startz = float(startz) if startz else None
  2734. self.z_end = float(endz) if endz else None
  2735. self.z_depthpercut = float(depthpercut) if depthpercut else None
  2736. self.multidepth = multidepth
  2737. self.z_toolchange = float(toolchangez) if toolchangez else None
  2738. try:
  2739. if toolchangexy == '':
  2740. self.xy_toolchange = None
  2741. else:
  2742. self.xy_toolchange = [float(eval(a)) for a in toolchangexy.split(",")]
  2743. if len(self.xy_toolchange) < 2:
  2744. self.app.inform.emit('[ERROR] %s' %
  2745. _("The Toolchange X,Y field in Edit -> Preferences has to be "
  2746. "in the format (x, y) \nbut now there is only one value, not two. "))
  2747. return 'fail'
  2748. except Exception as e:
  2749. log.debug("camlib.CNCJob.generate_from_geometry_2() --> %s" % str(e))
  2750. pass
  2751. self.pp_geometry_name = pp_geometry_name if pp_geometry_name else 'default'
  2752. self.f_plunge = self.app.defaults["geometry_f_plunge"]
  2753. if self.z_cut is None:
  2754. self.app.inform.emit('[ERROR_NOTCL] %s' %
  2755. _("Cut_Z parameter is None or zero. Most likely a bad combinations of "
  2756. "other parameters."))
  2757. return 'fail'
  2758. if self.z_cut > 0:
  2759. self.app.inform.emit('[WARNING] %s' %
  2760. _("The Cut Z parameter has positive value. "
  2761. "It is the depth value to cut into material.\n"
  2762. "The Cut Z parameter needs to have a negative value, assuming it is a typo "
  2763. "therefore the app will convert the value to negative."
  2764. "Check the resulting CNC code (Gcode etc)."))
  2765. self.z_cut = -self.z_cut
  2766. elif self.z_cut == 0:
  2767. self.app.inform.emit('[WARNING] %s: %s' %
  2768. (_("The Cut Z parameter is zero. There will be no cut, skipping file"),
  2769. geometry.options['name']))
  2770. return 'fail'
  2771. if self.z_move is None:
  2772. self.app.inform.emit('[ERROR_NOTCL] %s' %
  2773. _("Travel Z parameter is None or zero."))
  2774. return 'fail'
  2775. if self.z_move < 0:
  2776. self.app.inform.emit('[WARNING] %s' %
  2777. _("The Travel Z parameter has negative value. "
  2778. "It is the height value to travel between cuts.\n"
  2779. "The Z Travel parameter needs to have a positive value, assuming it is a typo "
  2780. "therefore the app will convert the value to positive."
  2781. "Check the resulting CNC code (Gcode etc)."))
  2782. self.z_move = -self.z_move
  2783. elif self.z_move == 0:
  2784. self.app.inform.emit('[WARNING] %s: %s' %
  2785. (_("The Z Travel parameter is zero. "
  2786. "This is dangerous, skipping file"), self.options['name']))
  2787. return 'fail'
  2788. # made sure that depth_per_cut is no more then the z_cut
  2789. if abs(self.z_cut) < self.z_depthpercut:
  2790. self.z_depthpercut = abs(self.z_cut)
  2791. # ## Index first and last points in paths
  2792. # What points to index.
  2793. def get_pts(o):
  2794. return [o.coords[0], o.coords[-1]]
  2795. # Create the indexed storage.
  2796. storage = FlatCAMRTreeStorage()
  2797. storage.get_points = get_pts
  2798. # Store the geometry
  2799. log.debug("Indexing geometry before generating G-Code...")
  2800. self.app.inform.emit(_("Indexing geometry before generating G-Code..."))
  2801. for shape in flat_geometry:
  2802. if self.app.abort_flag:
  2803. # graceful abort requested by the user
  2804. raise FlatCAMApp.GracefulException
  2805. if shape is not None: # TODO: This shouldn't have happened.
  2806. storage.insert(shape)
  2807. if not append:
  2808. self.gcode = ""
  2809. # tell postprocessor the number of tool (for toolchange)
  2810. self.tool = tool_no
  2811. # this is the tool diameter, it is used as such to accommodate the postprocessor who need the tool diameter
  2812. # given under the name 'toolC'
  2813. self.postdata['toolC'] = self.tooldia
  2814. # Initial G-Code
  2815. self.pp_geometry = self.app.postprocessors[self.pp_geometry_name]
  2816. p = self.pp_geometry
  2817. self.oldx = 0.0
  2818. self.oldy = 0.0
  2819. self.gcode = self.doformat(p.start_code)
  2820. self.gcode += self.doformat(p.feedrate_code) # sets the feed rate
  2821. if toolchange is False:
  2822. self.gcode += self.doformat(p.lift_code, x=self.oldx , y=self.oldy ) # Move (up) to travel height
  2823. self.gcode += self.doformat(p.startz_code, x=self.oldx , y=self.oldy )
  2824. if toolchange:
  2825. # if "line_xyz" in self.pp_geometry_name:
  2826. # self.gcode += self.doformat(p.toolchange_code, x=self.xy_toolchange[0], y=self.xy_toolchange[1])
  2827. # else:
  2828. # self.gcode += self.doformat(p.toolchange_code)
  2829. self.gcode += self.doformat(p.toolchange_code)
  2830. if 'laser' not in self.pp_geometry_name:
  2831. self.gcode += self.doformat(p.spindle_code) # Spindle start
  2832. else:
  2833. # for laser this will disable the laser
  2834. self.gcode += self.doformat(p.lift_code, x=self.oldx, y=self.oldy) # Move (up) to travel height
  2835. if self.dwell is True:
  2836. self.gcode += self.doformat(p.dwell_code) # Dwell time
  2837. else:
  2838. if 'laser' not in self.pp_geometry_name:
  2839. self.gcode += self.doformat(p.spindle_code) # Spindle start
  2840. if self.dwell is True:
  2841. self.gcode += self.doformat(p.dwell_code) # Dwell time
  2842. total_travel = 0.0
  2843. total_cut = 0.0
  2844. # Iterate over geometry paths getting the nearest each time.
  2845. log.debug("Starting G-Code...")
  2846. self.app.inform.emit(_("Starting G-Code..."))
  2847. # variables to display the percentage of work done
  2848. geo_len = len(flat_geometry)
  2849. disp_number = 0
  2850. old_disp_number = 0
  2851. log.warning("Number of paths for which to generate GCode: %s" % str(geo_len))
  2852. if self.units == 'MM':
  2853. current_tooldia = float('%.2f' % float(self.tooldia))
  2854. else:
  2855. current_tooldia = float('%.4f' % float(self.tooldia))
  2856. self.app.inform.emit(
  2857. '%s: %s%s.' % (_("Starting G-Code for tool with diameter"),
  2858. str(current_tooldia),
  2859. str(self.units))
  2860. )
  2861. path_count = 0
  2862. current_pt = (0, 0)
  2863. pt, geo = storage.nearest(current_pt)
  2864. try:
  2865. while True:
  2866. if self.app.abort_flag:
  2867. # graceful abort requested by the user
  2868. raise FlatCAMApp.GracefulException
  2869. path_count += 1
  2870. # Remove before modifying, otherwise deletion will fail.
  2871. storage.remove(geo)
  2872. # If last point in geometry is the nearest but prefer the first one if last point == first point
  2873. # then reverse coordinates.
  2874. if pt != geo.coords[0] and pt == geo.coords[-1]:
  2875. geo.coords = list(geo.coords)[::-1]
  2876. # ---------- Single depth/pass --------
  2877. if not multidepth:
  2878. # calculate the cut distance
  2879. total_cut += geo.length
  2880. self.gcode += self.create_gcode_single_pass(geo, extracut, tolerance, old_point=current_pt)
  2881. # --------- Multi-pass ---------
  2882. else:
  2883. # calculate the cut distance
  2884. # due of the number of cuts (multi depth) it has to multiplied by the number of cuts
  2885. nr_cuts = 0
  2886. depth = abs(self.z_cut)
  2887. while depth > 0:
  2888. nr_cuts += 1
  2889. depth -= float(self.z_depthpercut)
  2890. total_cut += (geo.length * nr_cuts)
  2891. self.gcode += self.create_gcode_multi_pass(geo, extracut, tolerance,
  2892. postproc=p, old_point=current_pt)
  2893. # calculate the travel distance
  2894. total_travel += abs(distance(pt1=current_pt, pt2=pt))
  2895. current_pt = geo.coords[-1]
  2896. pt, geo = storage.nearest(current_pt) # Next
  2897. disp_number = int(np.interp(path_count, [0, geo_len], [0, 100]))
  2898. if old_disp_number < disp_number <= 100:
  2899. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  2900. old_disp_number = disp_number
  2901. except StopIteration: # Nothing found in storage.
  2902. pass
  2903. log.debug("Finishing G-Code... %s paths traced." % path_count)
  2904. # add move to end position
  2905. total_travel += abs(distance_euclidian(current_pt[0], current_pt[1], 0, 0))
  2906. self.travel_distance += total_travel + total_cut
  2907. self.routing_time += total_cut / self.feedrate
  2908. # Finish
  2909. self.gcode += self.doformat(p.spindle_stop_code)
  2910. self.gcode += self.doformat(p.lift_code, x=current_pt[0], y=current_pt[1])
  2911. self.gcode += self.doformat(p.end_code, x=0, y=0)
  2912. self.app.inform.emit('%s... %s %s' %
  2913. (_("Finished G-Code generation"),
  2914. str(path_count),
  2915. _(" paths traced.")
  2916. )
  2917. )
  2918. return self.gcode
  2919. def generate_gcode_from_solderpaste_geo(self, **kwargs):
  2920. """
  2921. Algorithm to generate from multitool Geometry.
  2922. Algorithm description:
  2923. ----------------------
  2924. Uses RTree to find the nearest path to follow.
  2925. :return: Gcode string
  2926. """
  2927. log.debug("Generate_from_solderpaste_geometry()")
  2928. # ## Index first and last points in paths
  2929. # What points to index.
  2930. def get_pts(o):
  2931. return [o.coords[0], o.coords[-1]]
  2932. self.gcode = ""
  2933. if not kwargs:
  2934. log.debug("camlib.generate_from_solderpaste_geo() --> No tool in the solderpaste geometry.")
  2935. self.app.inform.emit('[ERROR_NOTCL] %s' %
  2936. _("There is no tool data in the SolderPaste geometry."))
  2937. # this is the tool diameter, it is used as such to accommodate the postprocessor who need the tool diameter
  2938. # given under the name 'toolC'
  2939. self.postdata['z_start'] = kwargs['data']['tools_solderpaste_z_start']
  2940. self.postdata['z_dispense'] = kwargs['data']['tools_solderpaste_z_dispense']
  2941. self.postdata['z_stop'] = kwargs['data']['tools_solderpaste_z_stop']
  2942. self.postdata['z_travel'] = kwargs['data']['tools_solderpaste_z_travel']
  2943. self.postdata['z_toolchange'] = kwargs['data']['tools_solderpaste_z_toolchange']
  2944. self.postdata['xy_toolchange'] = kwargs['data']['tools_solderpaste_xy_toolchange']
  2945. self.postdata['frxy'] = kwargs['data']['tools_solderpaste_frxy']
  2946. self.postdata['frz'] = kwargs['data']['tools_solderpaste_frz']
  2947. self.postdata['frz_dispense'] = kwargs['data']['tools_solderpaste_frz_dispense']
  2948. self.postdata['speedfwd'] = kwargs['data']['tools_solderpaste_speedfwd']
  2949. self.postdata['dwellfwd'] = kwargs['data']['tools_solderpaste_dwellfwd']
  2950. self.postdata['speedrev'] = kwargs['data']['tools_solderpaste_speedrev']
  2951. self.postdata['dwellrev'] = kwargs['data']['tools_solderpaste_dwellrev']
  2952. self.postdata['pp_solderpaste_name'] = kwargs['data']['tools_solderpaste_pp']
  2953. self.postdata['toolC'] = kwargs['tooldia']
  2954. self.pp_solderpaste_name = kwargs['data']['tools_solderpaste_pp'] if kwargs['data']['tools_solderpaste_pp'] \
  2955. else self.app.defaults['tools_solderpaste_pp']
  2956. p = self.app.postprocessors[self.pp_solderpaste_name]
  2957. # ## Flatten the geometry. Only linear elements (no polygons) remain.
  2958. flat_geometry = self.flatten(kwargs['solid_geometry'], pathonly=True)
  2959. log.debug("%d paths" % len(flat_geometry))
  2960. # Create the indexed storage.
  2961. storage = FlatCAMRTreeStorage()
  2962. storage.get_points = get_pts
  2963. # Store the geometry
  2964. log.debug("Indexing geometry before generating G-Code...")
  2965. for shape in flat_geometry:
  2966. if shape is not None:
  2967. storage.insert(shape)
  2968. # Initial G-Code
  2969. self.gcode = self.doformat(p.start_code)
  2970. self.gcode += self.doformat(p.spindle_off_code)
  2971. self.gcode += self.doformat(p.toolchange_code)
  2972. # ## Iterate over geometry paths getting the nearest each time.
  2973. log.debug("Starting SolderPaste G-Code...")
  2974. path_count = 0
  2975. current_pt = (0, 0)
  2976. # variables to display the percentage of work done
  2977. geo_len = len(flat_geometry)
  2978. disp_number = 0
  2979. old_disp_number = 0
  2980. pt, geo = storage.nearest(current_pt)
  2981. try:
  2982. while True:
  2983. if self.app.abort_flag:
  2984. # graceful abort requested by the user
  2985. raise FlatCAMApp.GracefulException
  2986. path_count += 1
  2987. # Remove before modifying, otherwise deletion will fail.
  2988. storage.remove(geo)
  2989. # If last point in geometry is the nearest but prefer the first one if last point == first point
  2990. # then reverse coordinates.
  2991. if pt != geo.coords[0] and pt == geo.coords[-1]:
  2992. geo.coords = list(geo.coords)[::-1]
  2993. self.gcode += self.create_soldepaste_gcode(geo, p=p, old_point=current_pt)
  2994. current_pt = geo.coords[-1]
  2995. pt, geo = storage.nearest(current_pt) # Next
  2996. disp_number = int(np.interp(path_count, [0, geo_len], [0, 100]))
  2997. if old_disp_number < disp_number <= 100:
  2998. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  2999. old_disp_number = disp_number
  3000. except StopIteration: # Nothing found in storage.
  3001. pass
  3002. log.debug("Finishing SolderPste G-Code... %s paths traced." % path_count)
  3003. self.app.inform.emit('%s... %s %s' %
  3004. (_("Finished SolderPste G-Code generation"),
  3005. str(path_count),
  3006. _("paths traced.")
  3007. )
  3008. )
  3009. # Finish
  3010. self.gcode += self.doformat(p.lift_code)
  3011. self.gcode += self.doformat(p.end_code)
  3012. return self.gcode
  3013. def create_soldepaste_gcode(self, geometry, p, old_point=(0, 0)):
  3014. gcode = ''
  3015. path = geometry.coords
  3016. self.coordinates_type = self.app.defaults["cncjob_coords_type"]
  3017. if self.coordinates_type == "G90":
  3018. # For Absolute coordinates type G90
  3019. first_x = path[0][0]
  3020. first_y = path[0][1]
  3021. else:
  3022. # For Incremental coordinates type G91
  3023. first_x = path[0][0] - old_point[0]
  3024. first_y = path[0][1] - old_point[1]
  3025. if type(geometry) == LineString or type(geometry) == LinearRing:
  3026. # Move fast to 1st point
  3027. gcode += self.doformat(p.rapid_code, x=first_x, y=first_y) # Move to first point
  3028. # Move down to cutting depth
  3029. gcode += self.doformat(p.z_feedrate_code)
  3030. gcode += self.doformat(p.down_z_start_code)
  3031. gcode += self.doformat(p.spindle_fwd_code) # Start dispensing
  3032. gcode += self.doformat(p.dwell_fwd_code)
  3033. gcode += self.doformat(p.feedrate_z_dispense_code)
  3034. gcode += self.doformat(p.lift_z_dispense_code)
  3035. gcode += self.doformat(p.feedrate_xy_code)
  3036. # Cutting...
  3037. prev_x = first_x
  3038. prev_y = first_y
  3039. for pt in path[1:]:
  3040. if self.coordinates_type == "G90":
  3041. # For Absolute coordinates type G90
  3042. next_x = pt[0]
  3043. next_y = pt[1]
  3044. else:
  3045. # For Incremental coordinates type G91
  3046. next_x = pt[0] - prev_x
  3047. next_y = pt[1] - prev_y
  3048. gcode += self.doformat(p.linear_code, x=next_x, y=next_y) # Linear motion to point
  3049. prev_x = next_x
  3050. prev_y = next_y
  3051. # Up to travelling height.
  3052. gcode += self.doformat(p.spindle_off_code) # Stop dispensing
  3053. gcode += self.doformat(p.spindle_rev_code)
  3054. gcode += self.doformat(p.down_z_stop_code)
  3055. gcode += self.doformat(p.spindle_off_code)
  3056. gcode += self.doformat(p.dwell_rev_code)
  3057. gcode += self.doformat(p.z_feedrate_code)
  3058. gcode += self.doformat(p.lift_code)
  3059. elif type(geometry) == Point:
  3060. gcode += self.doformat(p.linear_code, x=first_x, y=first_y) # Move to first point
  3061. gcode += self.doformat(p.feedrate_z_dispense_code)
  3062. gcode += self.doformat(p.down_z_start_code)
  3063. gcode += self.doformat(p.spindle_fwd_code) # Start dispensing
  3064. gcode += self.doformat(p.dwell_fwd_code)
  3065. gcode += self.doformat(p.lift_z_dispense_code)
  3066. gcode += self.doformat(p.spindle_off_code) # Stop dispensing
  3067. gcode += self.doformat(p.spindle_rev_code)
  3068. gcode += self.doformat(p.spindle_off_code)
  3069. gcode += self.doformat(p.down_z_stop_code)
  3070. gcode += self.doformat(p.dwell_rev_code)
  3071. gcode += self.doformat(p.z_feedrate_code)
  3072. gcode += self.doformat(p.lift_code)
  3073. return gcode
  3074. def create_gcode_single_pass(self, geometry, extracut, tolerance, old_point=(0, 0)):
  3075. # G-code. Note: self.linear2gcode() and self.point2gcode() will lower and raise the tool every time.
  3076. gcode_single_pass = ''
  3077. if type(geometry) == LineString or type(geometry) == LinearRing:
  3078. if extracut is False:
  3079. gcode_single_pass = self.linear2gcode(geometry, tolerance=tolerance, old_point=old_point)
  3080. else:
  3081. if geometry.is_ring:
  3082. gcode_single_pass = self.linear2gcode_extra(geometry, tolerance=tolerance, old_point=old_point)
  3083. else:
  3084. gcode_single_pass = self.linear2gcode(geometry, tolerance=tolerance, old_point=old_point)
  3085. elif type(geometry) == Point:
  3086. gcode_single_pass = self.point2gcode(geometry)
  3087. else:
  3088. log.warning("G-code generation not implemented for %s" % (str(type(geometry))))
  3089. return
  3090. return gcode_single_pass
  3091. def create_gcode_multi_pass(self, geometry, extracut, tolerance, postproc, old_point=(0, 0)):
  3092. gcode_multi_pass = ''
  3093. if isinstance(self.z_cut, Decimal):
  3094. z_cut = self.z_cut
  3095. else:
  3096. z_cut = Decimal(self.z_cut).quantize(Decimal('0.000000001'))
  3097. if self.z_depthpercut is None:
  3098. self.z_depthpercut = z_cut
  3099. elif not isinstance(self.z_depthpercut, Decimal):
  3100. self.z_depthpercut = Decimal(self.z_depthpercut).quantize(Decimal('0.000000001'))
  3101. depth = 0
  3102. reverse = False
  3103. while depth > z_cut:
  3104. # Increase depth. Limit to z_cut.
  3105. depth -= self.z_depthpercut
  3106. if depth < z_cut:
  3107. depth = z_cut
  3108. # Cut at specific depth and do not lift the tool.
  3109. # Note: linear2gcode() will use G00 to move to the first point in the path, but it should be already
  3110. # at the first point if the tool is down (in the material). So, an extra G00 should show up but
  3111. # is inconsequential.
  3112. if type(geometry) == LineString or type(geometry) == LinearRing:
  3113. if extracut is False:
  3114. gcode_multi_pass += self.linear2gcode(geometry, tolerance=tolerance, z_cut=depth, up=False,
  3115. old_point=old_point)
  3116. else:
  3117. if geometry.is_ring:
  3118. gcode_multi_pass += self.linear2gcode_extra(geometry, tolerance=tolerance, z_cut=depth,
  3119. up=False, old_point=old_point)
  3120. else:
  3121. gcode_multi_pass += self.linear2gcode(geometry, tolerance=tolerance, z_cut=depth, up=False,
  3122. old_point=old_point)
  3123. # Ignore multi-pass for points.
  3124. elif type(geometry) == Point:
  3125. gcode_multi_pass += self.point2gcode(geometry, old_point=old_point)
  3126. break # Ignoring ...
  3127. else:
  3128. log.warning("G-code generation not implemented for %s" % (str(type(geometry))))
  3129. # Reverse coordinates if not a loop so we can continue cutting without returning to the beginning.
  3130. if type(geometry) == LineString:
  3131. geometry.coords = list(geometry.coords)[::-1]
  3132. reverse = True
  3133. # If geometry is reversed, revert.
  3134. if reverse:
  3135. if type(geometry) == LineString:
  3136. geometry.coords = list(geometry.coords)[::-1]
  3137. # Lift the tool
  3138. gcode_multi_pass += self.doformat(postproc.lift_code, x=old_point[0], y=old_point[1])
  3139. return gcode_multi_pass
  3140. def codes_split(self, gline):
  3141. """
  3142. Parses a line of G-Code such as "G01 X1234 Y987" into
  3143. a dictionary: {'G': 1.0, 'X': 1234.0, 'Y': 987.0}
  3144. :param gline: G-Code line string
  3145. :return: Dictionary with parsed line.
  3146. """
  3147. command = {}
  3148. if 'Roland' in self.pp_excellon_name or 'Roland' in self.pp_geometry_name:
  3149. match_z = re.search(r"^Z(\s*-?\d+\.\d+?),(\s*\s*-?\d+\.\d+?),(\s*\s*-?\d+\.\d+?)*;$", gline)
  3150. if match_z:
  3151. command['G'] = 0
  3152. command['X'] = float(match_z.group(1).replace(" ", "")) * 0.025
  3153. command['Y'] = float(match_z.group(2).replace(" ", "")) * 0.025
  3154. command['Z'] = float(match_z.group(3).replace(" ", "")) * 0.025
  3155. elif 'hpgl' in self.pp_excellon_name or 'hpgl' in self.pp_geometry_name:
  3156. match_pa = re.search(r"^PA(\s*-?\d+\.\d+?),(\s*\s*-?\d+\.\d+?)*;$", gline)
  3157. if match_pa:
  3158. command['G'] = 0
  3159. command['X'] = float(match_pa.group(1).replace(" ", ""))
  3160. command['Y'] = float(match_pa.group(2).replace(" ", ""))
  3161. match_pen = re.search(r"^(P[U|D])", gline)
  3162. if match_pen:
  3163. if match_pen.group(1) == 'PU':
  3164. # the value does not matter, only that it is positive so the gcode_parse() know it is > 0,
  3165. # therefore the move is of kind T (travel)
  3166. command['Z'] = 1
  3167. else:
  3168. command['Z'] = 0
  3169. elif 'grbl_laser' in self.pp_excellon_name or 'grbl_laser' in self.pp_geometry_name or \
  3170. (self.pp_solderpaste_name is not None and 'Paste' in self.pp_solderpaste_name):
  3171. match_lsr = re.search(r"X([\+-]?\d+.[\+-]?\d+)\s*Y([\+-]?\d+.[\+-]?\d+)", gline)
  3172. if match_lsr:
  3173. command['X'] = float(match_lsr.group(1).replace(" ", ""))
  3174. command['Y'] = float(match_lsr.group(2).replace(" ", ""))
  3175. match_lsr_pos = re.search(r"^(M0[3|5])", gline)
  3176. if match_lsr_pos:
  3177. if 'M05' in match_lsr_pos.group(1):
  3178. # the value does not matter, only that it is positive so the gcode_parse() know it is > 0,
  3179. # therefore the move is of kind T (travel)
  3180. command['Z'] = 1
  3181. else:
  3182. command['Z'] = 0
  3183. elif self.pp_solderpaste_name is not None:
  3184. if 'Paste' in self.pp_solderpaste_name:
  3185. match_paste = re.search(r"X([\+-]?\d+.[\+-]?\d+)\s*Y([\+-]?\d+.[\+-]?\d+)", gline)
  3186. if match_paste:
  3187. command['X'] = float(match_paste.group(1).replace(" ", ""))
  3188. command['Y'] = float(match_paste.group(2).replace(" ", ""))
  3189. else:
  3190. match = re.search(r'^\s*([A-Z])\s*([\+\-\.\d\s]+)', gline)
  3191. while match:
  3192. command[match.group(1)] = float(match.group(2).replace(" ", ""))
  3193. gline = gline[match.end():]
  3194. match = re.search(r'^\s*([A-Z])\s*([\+\-\.\d\s]+)', gline)
  3195. return command
  3196. def gcode_parse(self):
  3197. """
  3198. G-Code parser (from self.gcode). Generates dictionary with
  3199. single-segment LineString's and "kind" indicating cut or travel,
  3200. fast or feedrate speed.
  3201. """
  3202. kind = ["C", "F"] # T=travel, C=cut, F=fast, S=slow
  3203. # Results go here
  3204. geometry = []
  3205. # Last known instruction
  3206. current = {'X': 0.0, 'Y': 0.0, 'Z': 0.0, 'G': 0}
  3207. # Current path: temporary storage until tool is
  3208. # lifted or lowered.
  3209. if self.toolchange_xy_type == "excellon":
  3210. if self.app.defaults["excellon_toolchangexy"] == '':
  3211. pos_xy = [0, 0]
  3212. else:
  3213. pos_xy = [float(eval(a)) for a in self.app.defaults["excellon_toolchangexy"].split(",")]
  3214. else:
  3215. if self.app.defaults["geometry_toolchangexy"] == '':
  3216. pos_xy = [0, 0]
  3217. else:
  3218. pos_xy = [float(eval(a)) for a in self.app.defaults["geometry_toolchangexy"].split(",")]
  3219. path = [pos_xy]
  3220. # path = [(0, 0)]
  3221. # Process every instruction
  3222. for line in StringIO(self.gcode):
  3223. if '%MO' in line or '%' in line or 'MOIN' in line or 'MOMM' in line:
  3224. return "fail"
  3225. gobj = self.codes_split(line)
  3226. # ## Units
  3227. if 'G' in gobj and (gobj['G'] == 20.0 or gobj['G'] == 21.0):
  3228. self.units = {20.0: "IN", 21.0: "MM"}[gobj['G']]
  3229. continue
  3230. # ## Changing height
  3231. if 'Z' in gobj:
  3232. if 'Roland' in self.pp_excellon_name or 'Roland' in self.pp_geometry_name:
  3233. pass
  3234. elif 'hpgl' in self.pp_excellon_name or 'hpgl' in self.pp_geometry_name:
  3235. pass
  3236. elif 'laser' in self.pp_excellon_name or 'laser' in self.pp_geometry_name:
  3237. pass
  3238. elif ('X' in gobj or 'Y' in gobj) and gobj['Z'] != current['Z']:
  3239. if self.pp_geometry_name == 'line_xyz' or self.pp_excellon_name == 'line_xyz':
  3240. pass
  3241. else:
  3242. log.warning("Non-orthogonal motion: From %s" % str(current))
  3243. log.warning(" To: %s" % str(gobj))
  3244. current['Z'] = gobj['Z']
  3245. # Store the path into geometry and reset path
  3246. if len(path) > 1:
  3247. geometry.append({"geom": LineString(path),
  3248. "kind": kind})
  3249. path = [path[-1]] # Start with the last point of last path.
  3250. # create the geometry for the holes created when drilling Excellon drills
  3251. if self.origin_kind == 'excellon':
  3252. if current['Z'] < 0:
  3253. current_drill_point_coords = (float('%.4f' % current['X']), float('%.4f' % current['Y']))
  3254. # find the drill diameter knowing the drill coordinates
  3255. for pt_dict in self.exc_drills:
  3256. point_in_dict_coords = (float('%.4f' % pt_dict['point'].x),
  3257. float('%.4f' % pt_dict['point'].y))
  3258. if point_in_dict_coords == current_drill_point_coords:
  3259. tool = pt_dict['tool']
  3260. dia = self.exc_tools[tool]['C']
  3261. kind = ['C', 'F']
  3262. geometry.append({"geom": Point(current_drill_point_coords).
  3263. buffer(dia/2).exterior,
  3264. "kind": kind})
  3265. break
  3266. if 'G' in gobj:
  3267. current['G'] = int(gobj['G'])
  3268. if 'X' in gobj or 'Y' in gobj:
  3269. if 'X' in gobj:
  3270. x = gobj['X']
  3271. # current['X'] = x
  3272. else:
  3273. x = current['X']
  3274. if 'Y' in gobj:
  3275. y = gobj['Y']
  3276. else:
  3277. y = current['Y']
  3278. kind = ["C", "F"] # T=travel, C=cut, F=fast, S=slow
  3279. if current['Z'] > 0:
  3280. kind[0] = 'T'
  3281. if current['G'] > 0:
  3282. kind[1] = 'S'
  3283. if current['G'] in [0, 1]: # line
  3284. path.append((x, y))
  3285. arcdir = [None, None, "cw", "ccw"]
  3286. if current['G'] in [2, 3]: # arc
  3287. center = [gobj['I'] + current['X'], gobj['J'] + current['Y']]
  3288. radius = sqrt(gobj['I']**2 + gobj['J']**2)
  3289. start = arctan2(-gobj['J'], -gobj['I'])
  3290. stop = arctan2(-center[1] + y, -center[0] + x)
  3291. path += arc(center, radius, start, stop, arcdir[current['G']], int(self.steps_per_circle / 4))
  3292. # Update current instruction
  3293. for code in gobj:
  3294. current[code] = gobj[code]
  3295. # There might not be a change in height at the
  3296. # end, therefore, see here too if there is
  3297. # a final path.
  3298. if len(path) > 1:
  3299. geometry.append({"geom": LineString(path),
  3300. "kind": kind})
  3301. self.gcode_parsed = geometry
  3302. return geometry
  3303. # def plot(self, tooldia=None, dpi=75, margin=0.1,
  3304. # color={"T": ["#F0E24D", "#B5AB3A"], "C": ["#5E6CFF", "#4650BD"]},
  3305. # alpha={"T": 0.3, "C": 1.0}):
  3306. # """
  3307. # Creates a Matplotlib figure with a plot of the
  3308. # G-code job.
  3309. # """
  3310. # if tooldia is None:
  3311. # tooldia = self.tooldia
  3312. #
  3313. # fig = Figure(dpi=dpi)
  3314. # ax = fig.add_subplot(111)
  3315. # ax.set_aspect(1)
  3316. # xmin, ymin, xmax, ymax = self.input_geometry_bounds
  3317. # ax.set_xlim(xmin-margin, xmax+margin)
  3318. # ax.set_ylim(ymin-margin, ymax+margin)
  3319. #
  3320. # if tooldia == 0:
  3321. # for geo in self.gcode_parsed:
  3322. # linespec = '--'
  3323. # linecolor = color[geo['kind'][0]][1]
  3324. # if geo['kind'][0] == 'C':
  3325. # linespec = 'k-'
  3326. # x, y = geo['geom'].coords.xy
  3327. # ax.plot(x, y, linespec, color=linecolor)
  3328. # else:
  3329. # for geo in self.gcode_parsed:
  3330. # poly = geo['geom'].buffer(tooldia/2.0)
  3331. # patch = PolygonPatch(poly, facecolor=color[geo['kind'][0]][0],
  3332. # edgecolor=color[geo['kind'][0]][1],
  3333. # alpha=alpha[geo['kind'][0]], zorder=2)
  3334. # ax.add_patch(patch)
  3335. #
  3336. # return fig
  3337. def plot2(self, tooldia=None, dpi=75, margin=0.1, gcode_parsed=None,
  3338. color={"T": ["#F0E24D4C", "#B5AB3A4C"], "C": ["#5E6CFFFF", "#4650BDFF"]},
  3339. alpha={"T": 0.3, "C": 1.0}, tool_tolerance=0.0005, obj=None, visible=False, kind='all'):
  3340. """
  3341. Plots the G-code job onto the given axes.
  3342. :param tooldia: Tool diameter.
  3343. :param dpi: Not used!
  3344. :param margin: Not used!
  3345. :param color: Color specification.
  3346. :param alpha: Transparency specification.
  3347. :param tool_tolerance: Tolerance when drawing the toolshape.
  3348. :param obj
  3349. :param visible
  3350. :param kind
  3351. :return: None
  3352. """
  3353. # units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  3354. gcode_parsed = gcode_parsed if gcode_parsed else self.gcode_parsed
  3355. path_num = 0
  3356. if tooldia is None:
  3357. tooldia = self.tooldia
  3358. # this should be unlikely unless when upstream the tooldia is a tuple made by one dia and a comma like (2.4,)
  3359. if isinstance(tooldia, list):
  3360. tooldia = tooldia[0] if tooldia[0] is not None else self.tooldia
  3361. if tooldia == 0:
  3362. for geo in gcode_parsed:
  3363. if kind == 'all':
  3364. obj.add_shape(shape=geo['geom'], color=color[geo['kind'][0]][1], visible=visible)
  3365. elif kind == 'travel':
  3366. if geo['kind'][0] == 'T':
  3367. obj.add_shape(shape=geo['geom'], color=color['T'][1], visible=visible)
  3368. elif kind == 'cut':
  3369. if geo['kind'][0] == 'C':
  3370. obj.add_shape(shape=geo['geom'], color=color['C'][1], visible=visible)
  3371. else:
  3372. text = []
  3373. pos = []
  3374. self.coordinates_type = self.app.defaults["cncjob_coords_type"]
  3375. if self.coordinates_type == "G90":
  3376. # For Absolute coordinates type G90
  3377. for geo in gcode_parsed:
  3378. if geo['kind'][0] == 'T':
  3379. current_position = geo['geom'].coords[0]
  3380. if current_position not in pos:
  3381. pos.append(current_position)
  3382. path_num += 1
  3383. text.append(str(path_num))
  3384. current_position = geo['geom'].coords[-1]
  3385. if current_position not in pos:
  3386. pos.append(current_position)
  3387. path_num += 1
  3388. text.append(str(path_num))
  3389. # plot the geometry of Excellon objects
  3390. if self.origin_kind == 'excellon':
  3391. try:
  3392. poly = Polygon(geo['geom'])
  3393. except ValueError:
  3394. # if the geos are travel lines it will enter into Exception
  3395. poly = geo['geom'].buffer(distance=(tooldia / 1.99999999), resolution=self.steps_per_circle)
  3396. poly = poly.simplify(tool_tolerance)
  3397. else:
  3398. # plot the geometry of any objects other than Excellon
  3399. poly = geo['geom'].buffer(distance=(tooldia / 1.99999999), resolution=self.steps_per_circle)
  3400. poly = poly.simplify(tool_tolerance)
  3401. if kind == 'all':
  3402. obj.add_shape(shape=poly, color=color[geo['kind'][0]][1], face_color=color[geo['kind'][0]][0],
  3403. visible=visible, layer=1 if geo['kind'][0] == 'C' else 2)
  3404. elif kind == 'travel':
  3405. if geo['kind'][0] == 'T':
  3406. obj.add_shape(shape=poly, color=color['T'][1], face_color=color['T'][0],
  3407. visible=visible, layer=2)
  3408. elif kind == 'cut':
  3409. if geo['kind'][0] == 'C':
  3410. obj.add_shape(shape=poly, color=color['C'][1], face_color=color['C'][0],
  3411. visible=visible, layer=1)
  3412. else:
  3413. # For Incremental coordinates type G91
  3414. self.app.inform.emit('[ERROR_NOTCL] %s' %
  3415. _('G91 coordinates not implemented ...'))
  3416. for geo in gcode_parsed:
  3417. if geo['kind'][0] == 'T':
  3418. current_position = geo['geom'].coords[0]
  3419. if current_position not in pos:
  3420. pos.append(current_position)
  3421. path_num += 1
  3422. text.append(str(path_num))
  3423. current_position = geo['geom'].coords[-1]
  3424. if current_position not in pos:
  3425. pos.append(current_position)
  3426. path_num += 1
  3427. text.append(str(path_num))
  3428. # plot the geometry of Excellon objects
  3429. if self.origin_kind == 'excellon':
  3430. try:
  3431. poly = Polygon(geo['geom'])
  3432. except ValueError:
  3433. # if the geos are travel lines it will enter into Exception
  3434. poly = geo['geom'].buffer(distance=(tooldia / 1.99999999), resolution=self.steps_per_circle)
  3435. poly = poly.simplify(tool_tolerance)
  3436. else:
  3437. # plot the geometry of any objects other than Excellon
  3438. poly = geo['geom'].buffer(distance=(tooldia / 1.99999999), resolution=self.steps_per_circle)
  3439. poly = poly.simplify(tool_tolerance)
  3440. if kind == 'all':
  3441. obj.add_shape(shape=poly, color=color[geo['kind'][0]][1], face_color=color[geo['kind'][0]][0],
  3442. visible=visible, layer=1 if geo['kind'][0] == 'C' else 2)
  3443. elif kind == 'travel':
  3444. if geo['kind'][0] == 'T':
  3445. obj.add_shape(shape=poly, color=color['T'][1], face_color=color['T'][0],
  3446. visible=visible, layer=2)
  3447. elif kind == 'cut':
  3448. if geo['kind'][0] == 'C':
  3449. obj.add_shape(shape=poly, color=color['C'][1], face_color=color['C'][0],
  3450. visible=visible, layer=1)
  3451. # current_x = gcode_parsed[0]['geom'].coords[0][0]
  3452. # current_y = gcode_parsed[0]['geom'].coords[0][1]
  3453. # old_pos = (
  3454. # current_x,
  3455. # current_y
  3456. # )
  3457. #
  3458. # for geo in gcode_parsed:
  3459. # if geo['kind'][0] == 'T':
  3460. # current_position = (
  3461. # geo['geom'].coords[0][0] + old_pos[0],
  3462. # geo['geom'].coords[0][1] + old_pos[1]
  3463. # )
  3464. # if current_position not in pos:
  3465. # pos.append(current_position)
  3466. # path_num += 1
  3467. # text.append(str(path_num))
  3468. #
  3469. # delta = (
  3470. # geo['geom'].coords[-1][0] - geo['geom'].coords[0][0],
  3471. # geo['geom'].coords[-1][1] - geo['geom'].coords[0][1]
  3472. # )
  3473. # current_position = (
  3474. # current_position[0] + geo['geom'].coords[-1][0],
  3475. # current_position[1] + geo['geom'].coords[-1][1]
  3476. # )
  3477. # if current_position not in pos:
  3478. # pos.append(current_position)
  3479. # path_num += 1
  3480. # text.append(str(path_num))
  3481. #
  3482. # # plot the geometry of Excellon objects
  3483. # if self.origin_kind == 'excellon':
  3484. # if isinstance(geo['geom'], Point):
  3485. # # if geo is Point
  3486. # current_position = (
  3487. # current_position[0] + geo['geom'].x,
  3488. # current_position[1] + geo['geom'].y
  3489. # )
  3490. # poly = Polygon(Point(current_position))
  3491. # elif isinstance(geo['geom'], LineString):
  3492. # # if the geos are travel lines (LineStrings)
  3493. # new_line_pts = []
  3494. # old_line_pos = deepcopy(current_position)
  3495. # for p in list(geo['geom'].coords):
  3496. # current_position = (
  3497. # current_position[0] + p[0],
  3498. # current_position[1] + p[1]
  3499. # )
  3500. # new_line_pts.append(current_position)
  3501. # old_line_pos = p
  3502. # new_line = LineString(new_line_pts)
  3503. #
  3504. # poly = new_line.buffer(distance=(tooldia / 1.99999999), resolution=self.steps_per_circle)
  3505. # poly = poly.simplify(tool_tolerance)
  3506. # else:
  3507. # # plot the geometry of any objects other than Excellon
  3508. # new_line_pts = []
  3509. # old_line_pos = deepcopy(current_position)
  3510. # for p in list(geo['geom'].coords):
  3511. # current_position = (
  3512. # current_position[0] + p[0],
  3513. # current_position[1] + p[1]
  3514. # )
  3515. # new_line_pts.append(current_position)
  3516. # old_line_pos = p
  3517. # new_line = LineString(new_line_pts)
  3518. #
  3519. # poly = new_line.buffer(distance=(tooldia / 1.99999999), resolution=self.steps_per_circle)
  3520. # poly = poly.simplify(tool_tolerance)
  3521. #
  3522. # old_pos = deepcopy(current_position)
  3523. #
  3524. # if kind == 'all':
  3525. # obj.add_shape(shape=poly, color=color[geo['kind'][0]][1], face_color=color[geo['kind'][0]][0],
  3526. # visible=visible, layer=1 if geo['kind'][0] == 'C' else 2)
  3527. # elif kind == 'travel':
  3528. # if geo['kind'][0] == 'T':
  3529. # obj.add_shape(shape=poly, color=color['T'][1], face_color=color['T'][0],
  3530. # visible=visible, layer=2)
  3531. # elif kind == 'cut':
  3532. # if geo['kind'][0] == 'C':
  3533. # obj.add_shape(shape=poly, color=color['C'][1], face_color=color['C'][0],
  3534. # visible=visible, layer=1)
  3535. try:
  3536. obj.annotation.set(text=text, pos=pos, visible=obj.options['plot'],
  3537. font_size=self.app.defaults["cncjob_annotation_fontsize"],
  3538. color=self.app.defaults["cncjob_annotation_fontcolor"])
  3539. except Exception as e:
  3540. pass
  3541. def create_geometry(self):
  3542. # TODO: This takes forever. Too much data?
  3543. self.solid_geometry = cascaded_union([geo['geom'] for geo in self.gcode_parsed])
  3544. return self.solid_geometry
  3545. # code snippet added by Lei Zheng in a rejected pull request on FlatCAM https://bitbucket.org/realthunder/
  3546. def segment(self, coords):
  3547. """
  3548. break long linear lines to make it more auto level friendly
  3549. """
  3550. if len(coords) < 2 or self.segx <= 0 and self.segy <= 0:
  3551. return list(coords)
  3552. path = [coords[0]]
  3553. # break the line in either x or y dimension only
  3554. def linebreak_single(line, dim, dmax):
  3555. if dmax <= 0:
  3556. return None
  3557. if line[1][dim] > line[0][dim]:
  3558. sign = 1.0
  3559. d = line[1][dim] - line[0][dim]
  3560. else:
  3561. sign = -1.0
  3562. d = line[0][dim] - line[1][dim]
  3563. if d > dmax:
  3564. # make sure we don't make any new lines too short
  3565. if d > dmax * 2:
  3566. dd = dmax
  3567. else:
  3568. dd = d / 2
  3569. other = dim ^ 1
  3570. return (line[0][dim] + dd * sign, line[0][other] + \
  3571. dd * (line[1][other] - line[0][other]) / d)
  3572. return None
  3573. # recursively breaks down a given line until it is within the
  3574. # required step size
  3575. def linebreak(line):
  3576. pt_new = linebreak_single(line, 0, self.segx)
  3577. if pt_new is None:
  3578. pt_new2 = linebreak_single(line, 1, self.segy)
  3579. else:
  3580. pt_new2 = linebreak_single((line[0], pt_new), 1, self.segy)
  3581. if pt_new2 is not None:
  3582. pt_new = pt_new2[::-1]
  3583. if pt_new is None:
  3584. path.append(line[1])
  3585. else:
  3586. path.append(pt_new)
  3587. linebreak((pt_new, line[1]))
  3588. for pt in coords[1:]:
  3589. linebreak((path[-1], pt))
  3590. return path
  3591. def linear2gcode(self, linear, tolerance=0, down=True, up=True,
  3592. z_cut=None, z_move=None, zdownrate=None,
  3593. feedrate=None, feedrate_z=None, feedrate_rapid=None, cont=False, old_point=(0, 0)):
  3594. """
  3595. Generates G-code to cut along the linear feature.
  3596. :param linear: The path to cut along.
  3597. :type: Shapely.LinearRing or Shapely.Linear String
  3598. :param tolerance: All points in the simplified object will be within the
  3599. tolerance distance of the original geometry.
  3600. :type tolerance: float
  3601. :param feedrate: speed for cut on X - Y plane
  3602. :param feedrate_z: speed for cut on Z plane
  3603. :param feedrate_rapid: speed to move between cuts; usually is G0 but some CNC require to specify it
  3604. :return: G-code to cut along the linear feature.
  3605. :rtype: str
  3606. """
  3607. if z_cut is None:
  3608. z_cut = self.z_cut
  3609. if z_move is None:
  3610. z_move = self.z_move
  3611. #
  3612. # if zdownrate is None:
  3613. # zdownrate = self.zdownrate
  3614. if feedrate is None:
  3615. feedrate = self.feedrate
  3616. if feedrate_z is None:
  3617. feedrate_z = self.z_feedrate
  3618. if feedrate_rapid is None:
  3619. feedrate_rapid = self.feedrate_rapid
  3620. # Simplify paths?
  3621. if tolerance > 0:
  3622. target_linear = linear.simplify(tolerance)
  3623. else:
  3624. target_linear = linear
  3625. gcode = ""
  3626. # path = list(target_linear.coords)
  3627. path = self.segment(target_linear.coords)
  3628. p = self.pp_geometry
  3629. self.coordinates_type = self.app.defaults["cncjob_coords_type"]
  3630. if self.coordinates_type == "G90":
  3631. # For Absolute coordinates type G90
  3632. first_x = path[0][0]
  3633. first_y = path[0][1]
  3634. else:
  3635. # For Incremental coordinates type G91
  3636. first_x = path[0][0] - old_point[0]
  3637. first_y = path[0][1] - old_point[1]
  3638. # Move fast to 1st point
  3639. if not cont:
  3640. gcode += self.doformat(p.rapid_code, x=first_x, y=first_y) # Move to first point
  3641. # Move down to cutting depth
  3642. if down:
  3643. # Different feedrate for vertical cut?
  3644. gcode += self.doformat(p.z_feedrate_code)
  3645. # gcode += self.doformat(p.feedrate_code)
  3646. gcode += self.doformat(p.down_code, x=first_x, y=first_y, z_cut=z_cut)
  3647. gcode += self.doformat(p.feedrate_code, feedrate=feedrate)
  3648. # Cutting...
  3649. prev_x = first_x
  3650. prev_y = first_y
  3651. for pt in path[1:]:
  3652. if self.app.abort_flag:
  3653. # graceful abort requested by the user
  3654. raise FlatCAMApp.GracefulException
  3655. if self.coordinates_type == "G90":
  3656. # For Absolute coordinates type G90
  3657. next_x = pt[0]
  3658. next_y = pt[1]
  3659. else:
  3660. # For Incremental coordinates type G91
  3661. # next_x = pt[0] - prev_x
  3662. # next_y = pt[1] - prev_y
  3663. self.app.inform.emit('[ERROR_NOTCL] %s' %
  3664. _('G91 coordinates not implemented ...'))
  3665. next_x = pt[0]
  3666. next_y = pt[1]
  3667. gcode += self.doformat(p.linear_code, x=next_x, y=next_y, z=z_cut) # Linear motion to point
  3668. prev_x = pt[0]
  3669. prev_y = pt[1]
  3670. # Up to travelling height.
  3671. if up:
  3672. gcode += self.doformat(p.lift_code, x=prev_x, y=prev_y, z_move=z_move) # Stop cutting
  3673. return gcode
  3674. def linear2gcode_extra(self, linear, tolerance=0, down=True, up=True,
  3675. z_cut=None, z_move=None, zdownrate=None,
  3676. feedrate=None, feedrate_z=None, feedrate_rapid=None, cont=False, old_point=(0, 0)):
  3677. """
  3678. Generates G-code to cut along the linear feature.
  3679. :param linear: The path to cut along.
  3680. :type: Shapely.LinearRing or Shapely.Linear String
  3681. :param tolerance: All points in the simplified object will be within the
  3682. tolerance distance of the original geometry.
  3683. :type tolerance: float
  3684. :param feedrate: speed for cut on X - Y plane
  3685. :param feedrate_z: speed for cut on Z plane
  3686. :param feedrate_rapid: speed to move between cuts; usually is G0 but some CNC require to specify it
  3687. :return: G-code to cut along the linear feature.
  3688. :rtype: str
  3689. """
  3690. if z_cut is None:
  3691. z_cut = self.z_cut
  3692. if z_move is None:
  3693. z_move = self.z_move
  3694. #
  3695. # if zdownrate is None:
  3696. # zdownrate = self.zdownrate
  3697. if feedrate is None:
  3698. feedrate = self.feedrate
  3699. if feedrate_z is None:
  3700. feedrate_z = self.z_feedrate
  3701. if feedrate_rapid is None:
  3702. feedrate_rapid = self.feedrate_rapid
  3703. # Simplify paths?
  3704. if tolerance > 0:
  3705. target_linear = linear.simplify(tolerance)
  3706. else:
  3707. target_linear = linear
  3708. gcode = ""
  3709. path = list(target_linear.coords)
  3710. p = self.pp_geometry
  3711. self.coordinates_type = self.app.defaults["cncjob_coords_type"]
  3712. if self.coordinates_type == "G90":
  3713. # For Absolute coordinates type G90
  3714. first_x = path[0][0]
  3715. first_y = path[0][1]
  3716. else:
  3717. # For Incremental coordinates type G91
  3718. first_x = path[0][0] - old_point[0]
  3719. first_y = path[0][1] - old_point[1]
  3720. # Move fast to 1st point
  3721. if not cont:
  3722. gcode += self.doformat(p.rapid_code, x=first_x, y=first_y) # Move to first point
  3723. # Move down to cutting depth
  3724. if down:
  3725. # Different feedrate for vertical cut?
  3726. if self.z_feedrate is not None:
  3727. gcode += self.doformat(p.z_feedrate_code)
  3728. # gcode += self.doformat(p.feedrate_code)
  3729. gcode += self.doformat(p.down_code, x=first_x, y=first_y, z_cut=z_cut)
  3730. gcode += self.doformat(p.feedrate_code, feedrate=feedrate)
  3731. else:
  3732. gcode += self.doformat(p.down_code, x=first_x, y=first_y, z_cut=z_cut) # Start cutting
  3733. # Cutting...
  3734. prev_x = first_x
  3735. prev_y = first_y
  3736. for pt in path[1:]:
  3737. if self.app.abort_flag:
  3738. # graceful abort requested by the user
  3739. raise FlatCAMApp.GracefulException
  3740. if self.coordinates_type == "G90":
  3741. # For Absolute coordinates type G90
  3742. next_x = pt[0]
  3743. next_y = pt[1]
  3744. else:
  3745. # For Incremental coordinates type G91
  3746. # For Incremental coordinates type G91
  3747. # next_x = pt[0] - prev_x
  3748. # next_y = pt[1] - prev_y
  3749. self.app.inform.emit('[ERROR_NOTCL] %s' %
  3750. _('G91 coordinates not implemented ...'))
  3751. next_x = pt[0]
  3752. next_y = pt[1]
  3753. gcode += self.doformat(p.linear_code, x=next_x, y=next_y, z=z_cut) # Linear motion to point
  3754. prev_x = pt[0]
  3755. prev_y = pt[1]
  3756. # this line is added to create an extra cut over the first point in patch
  3757. # to make sure that we remove the copper leftovers
  3758. # Linear motion to the 1st point in the cut path
  3759. if self.coordinates_type == "G90":
  3760. # For Absolute coordinates type G90
  3761. last_x = path[1][0]
  3762. last_y = path[1][1]
  3763. else:
  3764. # For Incremental coordinates type G91
  3765. last_x = path[1][0] - first_x
  3766. last_y = path[1][1] - first_y
  3767. gcode += self.doformat(p.linear_code, x=last_x, y=last_y)
  3768. # Up to travelling height.
  3769. if up:
  3770. gcode += self.doformat(p.lift_code, x=last_x, y=last_y, z_move=z_move) # Stop cutting
  3771. return gcode
  3772. def point2gcode(self, point, old_point=(0, 0)):
  3773. gcode = ""
  3774. if self.app.abort_flag:
  3775. # graceful abort requested by the user
  3776. raise FlatCAMApp.GracefulException
  3777. path = list(point.coords)
  3778. p = self.pp_geometry
  3779. self.coordinates_type = self.app.defaults["cncjob_coords_type"]
  3780. if self.coordinates_type == "G90":
  3781. # For Absolute coordinates type G90
  3782. first_x = path[0][0]
  3783. first_y = path[0][1]
  3784. else:
  3785. # For Incremental coordinates type G91
  3786. # first_x = path[0][0] - old_point[0]
  3787. # first_y = path[0][1] - old_point[1]
  3788. self.app.inform.emit('[ERROR_NOTCL] %s' %
  3789. _('G91 coordinates not implemented ...'))
  3790. first_x = path[0][0]
  3791. first_y = path[0][1]
  3792. gcode += self.doformat(p.linear_code, x=first_x, y=first_y) # Move to first point
  3793. if self.z_feedrate is not None:
  3794. gcode += self.doformat(p.z_feedrate_code)
  3795. gcode += self.doformat(p.down_code, x=first_x, y=first_y, z_cut = self.z_cut)
  3796. gcode += self.doformat(p.feedrate_code)
  3797. else:
  3798. gcode += self.doformat(p.down_code, x=first_x, y=first_y, z_cut = self.z_cut) # Start cutting
  3799. gcode += self.doformat(p.lift_code, x=first_x, y=first_y) # Stop cutting
  3800. return gcode
  3801. def export_svg(self, scale_factor=0.00):
  3802. """
  3803. Exports the CNC Job as a SVG Element
  3804. :scale_factor: float
  3805. :return: SVG Element string
  3806. """
  3807. # scale_factor is a multiplication factor for the SVG stroke-width used within shapely's svg export
  3808. # If not specified then try and use the tool diameter
  3809. # This way what is on screen will match what is outputed for the svg
  3810. # This is quite a useful feature for svg's used with visicut
  3811. if scale_factor <= 0:
  3812. scale_factor = self.options['tooldia'] / 2
  3813. # If still 0 then default to 0.05
  3814. # This value appears to work for zooming, and getting the output svg line width
  3815. # to match that viewed on screen with FlatCam
  3816. if scale_factor == 0:
  3817. scale_factor = 0.01
  3818. # Separate the list of cuts and travels into 2 distinct lists
  3819. # This way we can add different formatting / colors to both
  3820. cuts = []
  3821. travels = []
  3822. for g in self.gcode_parsed:
  3823. if self.app.abort_flag:
  3824. # graceful abort requested by the user
  3825. raise FlatCAMApp.GracefulException
  3826. if g['kind'][0] == 'C': cuts.append(g)
  3827. if g['kind'][0] == 'T': travels.append(g)
  3828. # Used to determine the overall board size
  3829. self.solid_geometry = cascaded_union([geo['geom'] for geo in self.gcode_parsed])
  3830. # Convert the cuts and travels into single geometry objects we can render as svg xml
  3831. if travels:
  3832. travelsgeom = cascaded_union([geo['geom'] for geo in travels])
  3833. if self.app.abort_flag:
  3834. # graceful abort requested by the user
  3835. raise FlatCAMApp.GracefulException
  3836. if cuts:
  3837. cutsgeom = cascaded_union([geo['geom'] for geo in cuts])
  3838. # Render the SVG Xml
  3839. # The scale factor affects the size of the lines, and the stroke color adds different formatting for each set
  3840. # It's better to have the travels sitting underneath the cuts for visicut
  3841. svg_elem = ""
  3842. if travels:
  3843. svg_elem = travelsgeom.svg(scale_factor=scale_factor, stroke_color="#F0E24D")
  3844. if cuts:
  3845. svg_elem += cutsgeom.svg(scale_factor=scale_factor, stroke_color="#5E6CFF")
  3846. return svg_elem
  3847. def bounds(self):
  3848. """
  3849. Returns coordinates of rectangular bounds
  3850. of geometry: (xmin, ymin, xmax, ymax).
  3851. """
  3852. # fixed issue of getting bounds only for one level lists of objects
  3853. # now it can get bounds for nested lists of objects
  3854. log.debug("camlib.CNCJob.bounds()")
  3855. def bounds_rec(obj):
  3856. if type(obj) is list:
  3857. minx = Inf
  3858. miny = Inf
  3859. maxx = -Inf
  3860. maxy = -Inf
  3861. for k in obj:
  3862. if type(k) is dict:
  3863. for key in k:
  3864. minx_, miny_, maxx_, maxy_ = bounds_rec(k[key])
  3865. minx = min(minx, minx_)
  3866. miny = min(miny, miny_)
  3867. maxx = max(maxx, maxx_)
  3868. maxy = max(maxy, maxy_)
  3869. else:
  3870. minx_, miny_, maxx_, maxy_ = bounds_rec(k)
  3871. minx = min(minx, minx_)
  3872. miny = min(miny, miny_)
  3873. maxx = max(maxx, maxx_)
  3874. maxy = max(maxy, maxy_)
  3875. return minx, miny, maxx, maxy
  3876. else:
  3877. # it's a Shapely object, return it's bounds
  3878. return obj.bounds
  3879. if self.multitool is False:
  3880. log.debug("CNCJob->bounds()")
  3881. if self.solid_geometry is None:
  3882. log.debug("solid_geometry is None")
  3883. return 0, 0, 0, 0
  3884. bounds_coords = bounds_rec(self.solid_geometry)
  3885. else:
  3886. minx = Inf
  3887. miny = Inf
  3888. maxx = -Inf
  3889. maxy = -Inf
  3890. for k, v in self.cnc_tools.items():
  3891. minx = Inf
  3892. miny = Inf
  3893. maxx = -Inf
  3894. maxy = -Inf
  3895. try:
  3896. for k in v['solid_geometry']:
  3897. minx_, miny_, maxx_, maxy_ = bounds_rec(k)
  3898. minx = min(minx, minx_)
  3899. miny = min(miny, miny_)
  3900. maxx = max(maxx, maxx_)
  3901. maxy = max(maxy, maxy_)
  3902. except TypeError:
  3903. minx_, miny_, maxx_, maxy_ = bounds_rec(v['solid_geometry'])
  3904. minx = min(minx, minx_)
  3905. miny = min(miny, miny_)
  3906. maxx = max(maxx, maxx_)
  3907. maxy = max(maxy, maxy_)
  3908. bounds_coords = minx, miny, maxx, maxy
  3909. return bounds_coords
  3910. # TODO This function should be replaced at some point with a "real" function. Until then it's an ugly hack ...
  3911. def scale(self, xfactor, yfactor=None, point=None):
  3912. """
  3913. Scales all the geometry on the XY plane in the object by the
  3914. given factor. Tool sizes, feedrates, or Z-axis dimensions are
  3915. not altered.
  3916. :param factor: Number by which to scale the object.
  3917. :type factor: float
  3918. :param point: the (x,y) coords for the point of origin of scale
  3919. :type tuple of floats
  3920. :return: None
  3921. :rtype: None
  3922. """
  3923. log.debug("camlib.CNCJob.scale()")
  3924. if yfactor is None:
  3925. yfactor = xfactor
  3926. if point is None:
  3927. px = 0
  3928. py = 0
  3929. else:
  3930. px, py = point
  3931. def scale_g(g):
  3932. """
  3933. :param g: 'g' parameter it's a gcode string
  3934. :return: scaled gcode string
  3935. """
  3936. temp_gcode = ''
  3937. header_start = False
  3938. header_stop = False
  3939. units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  3940. lines = StringIO(g)
  3941. for line in lines:
  3942. # this changes the GCODE header ---- UGLY HACK
  3943. if "TOOL DIAMETER" in line or "Feedrate:" in line:
  3944. header_start = True
  3945. if "G20" in line or "G21" in line:
  3946. header_start = False
  3947. header_stop = True
  3948. if header_start is True:
  3949. header_stop = False
  3950. if "in" in line:
  3951. if units == 'MM':
  3952. line = line.replace("in", "mm")
  3953. if "mm" in line:
  3954. if units == 'IN':
  3955. line = line.replace("mm", "in")
  3956. # find any float number in header (even multiple on the same line) and convert it
  3957. numbers_in_header = re.findall(self.g_nr_re, line)
  3958. if numbers_in_header:
  3959. for nr in numbers_in_header:
  3960. new_nr = float(nr) * xfactor
  3961. # replace the updated string
  3962. line = line.replace(nr, ('%.*f' % (self.app.defaults["cncjob_coords_decimals"], new_nr))
  3963. )
  3964. # this scales all the X and Y and Z and F values and also the Tool Dia in the toolchange message
  3965. if header_stop is True:
  3966. if "G20" in line:
  3967. if units == 'MM':
  3968. line = line.replace("G20", "G21")
  3969. if "G21" in line:
  3970. if units == 'IN':
  3971. line = line.replace("G21", "G20")
  3972. # find the X group
  3973. match_x = self.g_x_re.search(line)
  3974. if match_x:
  3975. if match_x.group(1) is not None:
  3976. new_x = float(match_x.group(1)[1:]) * xfactor
  3977. # replace the updated string
  3978. line = line.replace(
  3979. match_x.group(1),
  3980. 'X%.*f' % (self.app.defaults["cncjob_coords_decimals"], new_x)
  3981. )
  3982. # find the Y group
  3983. match_y = self.g_y_re.search(line)
  3984. if match_y:
  3985. if match_y.group(1) is not None:
  3986. new_y = float(match_y.group(1)[1:]) * yfactor
  3987. line = line.replace(
  3988. match_y.group(1),
  3989. 'Y%.*f' % (self.app.defaults["cncjob_coords_decimals"], new_y)
  3990. )
  3991. # find the Z group
  3992. match_z = self.g_z_re.search(line)
  3993. if match_z:
  3994. if match_z.group(1) is not None:
  3995. new_z = float(match_z.group(1)[1:]) * xfactor
  3996. line = line.replace(
  3997. match_z.group(1),
  3998. 'Z%.*f' % (self.app.defaults["cncjob_coords_decimals"], new_z)
  3999. )
  4000. # find the F group
  4001. match_f = self.g_f_re.search(line)
  4002. if match_f:
  4003. if match_f.group(1) is not None:
  4004. new_f = float(match_f.group(1)[1:]) * xfactor
  4005. line = line.replace(
  4006. match_f.group(1),
  4007. 'F%.*f' % (self.app.defaults["cncjob_fr_decimals"], new_f)
  4008. )
  4009. # find the T group (tool dia on toolchange)
  4010. match_t = self.g_t_re.search(line)
  4011. if match_t:
  4012. if match_t.group(1) is not None:
  4013. new_t = float(match_t.group(1)[1:]) * xfactor
  4014. line = line.replace(
  4015. match_t.group(1),
  4016. '= %.*f' % (self.app.defaults["cncjob_coords_decimals"], new_t)
  4017. )
  4018. temp_gcode += line
  4019. lines.close()
  4020. header_stop = False
  4021. return temp_gcode
  4022. if self.multitool is False:
  4023. # offset Gcode
  4024. self.gcode = scale_g(self.gcode)
  4025. # variables to display the percentage of work done
  4026. self.geo_len = 0
  4027. try:
  4028. for g in self.gcode_parsed:
  4029. self.geo_len += 1
  4030. except TypeError:
  4031. self.geo_len = 1
  4032. self.old_disp_number = 0
  4033. self.el_count = 0
  4034. # scale geometry
  4035. for g in self.gcode_parsed:
  4036. try:
  4037. g['geom'] = affinity.scale(g['geom'], xfactor, yfactor, origin=(px, py))
  4038. except AttributeError:
  4039. return g['geom']
  4040. self.el_count += 1
  4041. disp_number = int(np.interp(self.el_count, [0, self.geo_len], [0, 100]))
  4042. if self.old_disp_number < disp_number <= 100:
  4043. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  4044. self.old_disp_number = disp_number
  4045. self.create_geometry()
  4046. else:
  4047. for k, v in self.cnc_tools.items():
  4048. # scale Gcode
  4049. v['gcode'] = scale_g(v['gcode'])
  4050. # variables to display the percentage of work done
  4051. self.geo_len = 0
  4052. try:
  4053. for g in v['gcode_parsed']:
  4054. self.geo_len += 1
  4055. except TypeError:
  4056. self.geo_len = 1
  4057. self.old_disp_number = 0
  4058. self.el_count = 0
  4059. # scale gcode_parsed
  4060. for g in v['gcode_parsed']:
  4061. try:
  4062. g['geom'] = affinity.scale(g['geom'], xfactor, yfactor, origin=(px, py))
  4063. except AttributeError:
  4064. return g['geom']
  4065. self.el_count += 1
  4066. disp_number = int(np.interp(self.el_count, [0, self.geo_len], [0, 100]))
  4067. if self.old_disp_number < disp_number <= 100:
  4068. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  4069. self.old_disp_number = disp_number
  4070. v['solid_geometry'] = cascaded_union([geo['geom'] for geo in v['gcode_parsed']])
  4071. self.create_geometry()
  4072. self.app.proc_container.new_text = ''
  4073. def offset(self, vect):
  4074. """
  4075. Offsets all the geometry on the XY plane in the object by the
  4076. given vector.
  4077. Offsets all the GCODE on the XY plane in the object by the
  4078. given vector.
  4079. g_offsetx_re, g_offsety_re, multitool, cnnc_tools are attributes of FlatCAMCNCJob class in camlib
  4080. :param vect: (x, y) offset vector.
  4081. :type vect: tuple
  4082. :return: None
  4083. """
  4084. log.debug("camlib.CNCJob.offset()")
  4085. dx, dy = vect
  4086. def offset_g(g):
  4087. """
  4088. :param g: 'g' parameter it's a gcode string
  4089. :return: offseted gcode string
  4090. """
  4091. temp_gcode = ''
  4092. lines = StringIO(g)
  4093. for line in lines:
  4094. # find the X group
  4095. match_x = self.g_x_re.search(line)
  4096. if match_x:
  4097. if match_x.group(1) is not None:
  4098. # get the coordinate and add X offset
  4099. new_x = float(match_x.group(1)[1:]) + dx
  4100. # replace the updated string
  4101. line = line.replace(
  4102. match_x.group(1),
  4103. 'X%.*f' % (self.app.defaults["cncjob_coords_decimals"], new_x)
  4104. )
  4105. match_y = self.g_y_re.search(line)
  4106. if match_y:
  4107. if match_y.group(1) is not None:
  4108. new_y = float(match_y.group(1)[1:]) + dy
  4109. line = line.replace(
  4110. match_y.group(1),
  4111. 'Y%.*f' % (self.app.defaults["cncjob_coords_decimals"], new_y)
  4112. )
  4113. temp_gcode += line
  4114. lines.close()
  4115. return temp_gcode
  4116. if self.multitool is False:
  4117. # offset Gcode
  4118. self.gcode = offset_g(self.gcode)
  4119. # variables to display the percentage of work done
  4120. self.geo_len = 0
  4121. try:
  4122. for g in self.gcode_parsed:
  4123. self.geo_len += 1
  4124. except TypeError:
  4125. self.geo_len = 1
  4126. self.old_disp_number = 0
  4127. self.el_count = 0
  4128. # offset geometry
  4129. for g in self.gcode_parsed:
  4130. try:
  4131. g['geom'] = affinity.translate(g['geom'], xoff=dx, yoff=dy)
  4132. except AttributeError:
  4133. return g['geom']
  4134. self.el_count += 1
  4135. disp_number = int(np.interp(self.el_count, [0, self.geo_len], [0, 100]))
  4136. if self.old_disp_number < disp_number <= 100:
  4137. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  4138. self.old_disp_number = disp_number
  4139. self.create_geometry()
  4140. else:
  4141. for k, v in self.cnc_tools.items():
  4142. # offset Gcode
  4143. v['gcode'] = offset_g(v['gcode'])
  4144. # variables to display the percentage of work done
  4145. self.geo_len = 0
  4146. try:
  4147. for g in v['gcode_parsed']:
  4148. self.geo_len += 1
  4149. except TypeError:
  4150. self.geo_len = 1
  4151. self.old_disp_number = 0
  4152. self.el_count = 0
  4153. # offset gcode_parsed
  4154. for g in v['gcode_parsed']:
  4155. try:
  4156. g['geom'] = affinity.translate(g['geom'], xoff=dx, yoff=dy)
  4157. except AttributeError:
  4158. return g['geom']
  4159. self.el_count += 1
  4160. disp_number = int(np.interp(self.el_count, [0, self.geo_len], [0, 100]))
  4161. if self.old_disp_number < disp_number <= 100:
  4162. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  4163. self.old_disp_number = disp_number
  4164. # for the bounding box
  4165. v['solid_geometry'] = cascaded_union([geo['geom'] for geo in v['gcode_parsed']])
  4166. self.app.proc_container.new_text = ''
  4167. def mirror(self, axis, point):
  4168. """
  4169. Mirror the geometrys of an object by an given axis around the coordinates of the 'point'
  4170. :param angle:
  4171. :param point: tupple of coordinates (x,y)
  4172. :return:
  4173. """
  4174. log.debug("camlib.CNCJob.mirror()")
  4175. px, py = point
  4176. xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
  4177. # variables to display the percentage of work done
  4178. self.geo_len = 0
  4179. try:
  4180. for g in self.gcode_parsed:
  4181. self.geo_len += 1
  4182. except TypeError:
  4183. self.geo_len = 1
  4184. self.old_disp_number = 0
  4185. self.el_count = 0
  4186. for g in self.gcode_parsed:
  4187. try:
  4188. g['geom'] = affinity.scale(g['geom'], xscale, yscale, origin=(px, py))
  4189. except AttributeError:
  4190. return g['geom']
  4191. self.el_count += 1
  4192. disp_number = int(np.interp(self.el_count, [0, self.geo_len], [0, 100]))
  4193. if self.old_disp_number < disp_number <= 100:
  4194. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  4195. self.old_disp_number = disp_number
  4196. self.create_geometry()
  4197. self.app.proc_container.new_text = ''
  4198. def skew(self, angle_x, angle_y, point):
  4199. """
  4200. Shear/Skew the geometries of an object by angles along x and y dimensions.
  4201. Parameters
  4202. ----------
  4203. angle_x, angle_y : float, float
  4204. The shear angle(s) for the x and y axes respectively. These can be
  4205. specified in either degrees (default) or radians by setting
  4206. use_radians=True.
  4207. point: tupple of coordinates (x,y)
  4208. See shapely manual for more information:
  4209. http://toblerity.org/shapely/manual.html#affine-transformations
  4210. """
  4211. log.debug("camlib.CNCJob.skew()")
  4212. px, py = point
  4213. # variables to display the percentage of work done
  4214. self.geo_len = 0
  4215. try:
  4216. for g in self.gcode_parsed:
  4217. self.geo_len += 1
  4218. except TypeError:
  4219. self.geo_len = 1
  4220. self.old_disp_number = 0
  4221. self.el_count = 0
  4222. for g in self.gcode_parsed:
  4223. try:
  4224. g['geom'] = affinity.skew(g['geom'], angle_x, angle_y, origin=(px, py))
  4225. except AttributeError:
  4226. return g['geom']
  4227. self.el_count += 1
  4228. disp_number = int(np.interp(self.el_count, [0, self.geo_len], [0, 100]))
  4229. if self.old_disp_number < disp_number <= 100:
  4230. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  4231. self.old_disp_number = disp_number
  4232. self.create_geometry()
  4233. self.app.proc_container.new_text = ''
  4234. def rotate(self, angle, point):
  4235. """
  4236. Rotate the geometrys of an object by an given angle around the coordinates of the 'point'
  4237. :param angle:
  4238. :param point: tupple of coordinates (x,y)
  4239. :return:
  4240. """
  4241. log.debug("camlib.CNCJob.rotate()")
  4242. px, py = point
  4243. # variables to display the percentage of work done
  4244. self.geo_len = 0
  4245. try:
  4246. for g in self.gcode_parsed:
  4247. self.geo_len += 1
  4248. except TypeError:
  4249. self.geo_len = 1
  4250. self.old_disp_number = 0
  4251. self.el_count = 0
  4252. for g in self.gcode_parsed:
  4253. try:
  4254. g['geom'] = affinity.rotate(g['geom'], angle, origin=(px, py))
  4255. except AttributeError:
  4256. return g['geom']
  4257. self.el_count += 1
  4258. disp_number = int(np.interp(self.el_count, [0, self.geo_len], [0, 100]))
  4259. if self.old_disp_number < disp_number <= 100:
  4260. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  4261. self.old_disp_number = disp_number
  4262. self.create_geometry()
  4263. self.app.proc_container.new_text = ''
  4264. def get_bounds(geometry_list):
  4265. xmin = Inf
  4266. ymin = Inf
  4267. xmax = -Inf
  4268. ymax = -Inf
  4269. for gs in geometry_list:
  4270. try:
  4271. gxmin, gymin, gxmax, gymax = gs.bounds()
  4272. xmin = min([xmin, gxmin])
  4273. ymin = min([ymin, gymin])
  4274. xmax = max([xmax, gxmax])
  4275. ymax = max([ymax, gymax])
  4276. except:
  4277. log.warning("DEVELOPMENT: Tried to get bounds of empty geometry.")
  4278. return [xmin, ymin, xmax, ymax]
  4279. def arc(center, radius, start, stop, direction, steps_per_circ):
  4280. """
  4281. Creates a list of point along the specified arc.
  4282. :param center: Coordinates of the center [x, y]
  4283. :type center: list
  4284. :param radius: Radius of the arc.
  4285. :type radius: float
  4286. :param start: Starting angle in radians
  4287. :type start: float
  4288. :param stop: End angle in radians
  4289. :type stop: float
  4290. :param direction: Orientation of the arc, "CW" or "CCW"
  4291. :type direction: string
  4292. :param steps_per_circ: Number of straight line segments to
  4293. represent a circle.
  4294. :type steps_per_circ: int
  4295. :return: The desired arc, as list of tuples
  4296. :rtype: list
  4297. """
  4298. # TODO: Resolution should be established by maximum error from the exact arc.
  4299. da_sign = {"cw": -1.0, "ccw": 1.0}
  4300. points = []
  4301. if direction == "ccw" and stop <= start:
  4302. stop += 2 * pi
  4303. if direction == "cw" and stop >= start:
  4304. stop -= 2 * pi
  4305. angle = abs(stop - start)
  4306. #angle = stop-start
  4307. steps = max([int(ceil(angle / (2 * pi) * steps_per_circ)), 2])
  4308. delta_angle = da_sign[direction] * angle * 1.0 / steps
  4309. for i in range(steps + 1):
  4310. theta = start + delta_angle * i
  4311. points.append((center[0] + radius * cos(theta), center[1] + radius * sin(theta)))
  4312. return points
  4313. def arc2(p1, p2, center, direction, steps_per_circ):
  4314. r = sqrt((center[0] - p1[0]) ** 2 + (center[1] - p1[1]) ** 2)
  4315. start = arctan2(p1[1] - center[1], p1[0] - center[0])
  4316. stop = arctan2(p2[1] - center[1], p2[0] - center[0])
  4317. return arc(center, r, start, stop, direction, steps_per_circ)
  4318. def arc_angle(start, stop, direction):
  4319. if direction == "ccw" and stop <= start:
  4320. stop += 2 * pi
  4321. if direction == "cw" and stop >= start:
  4322. stop -= 2 * pi
  4323. angle = abs(stop - start)
  4324. return angle
  4325. # def find_polygon(poly, point):
  4326. # """
  4327. # Find an object that object.contains(Point(point)) in
  4328. # poly, which can can be iterable, contain iterable of, or
  4329. # be itself an implementer of .contains().
  4330. #
  4331. # :param poly: See description
  4332. # :return: Polygon containing point or None.
  4333. # """
  4334. #
  4335. # if poly is None:
  4336. # return None
  4337. #
  4338. # try:
  4339. # for sub_poly in poly:
  4340. # p = find_polygon(sub_poly, point)
  4341. # if p is not None:
  4342. # return p
  4343. # except TypeError:
  4344. # try:
  4345. # if poly.contains(Point(point)):
  4346. # return poly
  4347. # except AttributeError:
  4348. # return None
  4349. #
  4350. # return None
  4351. def to_dict(obj):
  4352. """
  4353. Makes the following types into serializable form:
  4354. * ApertureMacro
  4355. * BaseGeometry
  4356. :param obj: Shapely geometry.
  4357. :type obj: BaseGeometry
  4358. :return: Dictionary with serializable form if ``obj`` was
  4359. BaseGeometry or ApertureMacro, otherwise returns ``obj``.
  4360. """
  4361. if isinstance(obj, ApertureMacro):
  4362. return {
  4363. "__class__": "ApertureMacro",
  4364. "__inst__": obj.to_dict()
  4365. }
  4366. if isinstance(obj, BaseGeometry):
  4367. return {
  4368. "__class__": "Shply",
  4369. "__inst__": sdumps(obj)
  4370. }
  4371. return obj
  4372. def dict2obj(d):
  4373. """
  4374. Default deserializer.
  4375. :param d: Serializable dictionary representation of an object
  4376. to be reconstructed.
  4377. :return: Reconstructed object.
  4378. """
  4379. if '__class__' in d and '__inst__' in d:
  4380. if d['__class__'] == "Shply":
  4381. return sloads(d['__inst__'])
  4382. if d['__class__'] == "ApertureMacro":
  4383. am = ApertureMacro()
  4384. am.from_dict(d['__inst__'])
  4385. return am
  4386. return d
  4387. else:
  4388. return d
  4389. # def plotg(geo, solid_poly=False, color="black"):
  4390. # try:
  4391. # __ = iter(geo)
  4392. # except:
  4393. # geo = [geo]
  4394. #
  4395. # for g in geo:
  4396. # if type(g) == Polygon:
  4397. # if solid_poly:
  4398. # patch = PolygonPatch(g,
  4399. # facecolor="#BBF268",
  4400. # edgecolor="#006E20",
  4401. # alpha=0.75,
  4402. # zorder=2)
  4403. # ax = subplot(111)
  4404. # ax.add_patch(patch)
  4405. # else:
  4406. # x, y = g.exterior.coords.xy
  4407. # plot(x, y, color=color)
  4408. # for ints in g.interiors:
  4409. # x, y = ints.coords.xy
  4410. # plot(x, y, color=color)
  4411. # continue
  4412. #
  4413. # if type(g) == LineString or type(g) == LinearRing:
  4414. # x, y = g.coords.xy
  4415. # plot(x, y, color=color)
  4416. # continue
  4417. #
  4418. # if type(g) == Point:
  4419. # x, y = g.coords.xy
  4420. # plot(x, y, 'o')
  4421. # continue
  4422. #
  4423. # try:
  4424. # __ = iter(g)
  4425. # plotg(g, color=color)
  4426. # except:
  4427. # log.error("Cannot plot: " + str(type(g)))
  4428. # continue
  4429. def parse_gerber_number(strnumber, int_digits, frac_digits, zeros):
  4430. """
  4431. Parse a single number of Gerber coordinates.
  4432. :param strnumber: String containing a number in decimal digits
  4433. from a coordinate data block, possibly with a leading sign.
  4434. :type strnumber: str
  4435. :param int_digits: Number of digits used for the integer
  4436. part of the number
  4437. :type frac_digits: int
  4438. :param frac_digits: Number of digits used for the fractional
  4439. part of the number
  4440. :type frac_digits: int
  4441. :param zeros: If 'L', leading zeros are removed and trailing zeros are kept. Same situation for 'D' when
  4442. no zero suppression is done. If 'T', is in reverse.
  4443. :type zeros: str
  4444. :return: The number in floating point.
  4445. :rtype: float
  4446. """
  4447. ret_val = None
  4448. if zeros == 'L' or zeros == 'D':
  4449. ret_val = int(strnumber) * (10 ** (-frac_digits))
  4450. if zeros == 'T':
  4451. int_val = int(strnumber)
  4452. ret_val = (int_val * (10 ** ((int_digits + frac_digits) - len(strnumber)))) * (10 ** (-frac_digits))
  4453. return ret_val
  4454. # def alpha_shape(points, alpha):
  4455. # """
  4456. # Compute the alpha shape (concave hull) of a set of points.
  4457. #
  4458. # @param points: Iterable container of points.
  4459. # @param alpha: alpha value to influence the gooeyness of the border. Smaller
  4460. # numbers don't fall inward as much as larger numbers. Too large,
  4461. # and you lose everything!
  4462. # """
  4463. # if len(points) < 4:
  4464. # # When you have a triangle, there is no sense in computing an alpha
  4465. # # shape.
  4466. # return MultiPoint(list(points)).convex_hull
  4467. #
  4468. # def add_edge(edges, edge_points, coords, i, j):
  4469. # """Add a line between the i-th and j-th points, if not in the list already"""
  4470. # if (i, j) in edges or (j, i) in edges:
  4471. # # already added
  4472. # return
  4473. # edges.add( (i, j) )
  4474. # edge_points.append(coords[ [i, j] ])
  4475. #
  4476. # coords = np.array([point.coords[0] for point in points])
  4477. #
  4478. # tri = Delaunay(coords)
  4479. # edges = set()
  4480. # edge_points = []
  4481. # # loop over triangles:
  4482. # # ia, ib, ic = indices of corner points of the triangle
  4483. # for ia, ib, ic in tri.vertices:
  4484. # pa = coords[ia]
  4485. # pb = coords[ib]
  4486. # pc = coords[ic]
  4487. #
  4488. # # Lengths of sides of triangle
  4489. # a = math.sqrt((pa[0]-pb[0])**2 + (pa[1]-pb[1])**2)
  4490. # b = math.sqrt((pb[0]-pc[0])**2 + (pb[1]-pc[1])**2)
  4491. # c = math.sqrt((pc[0]-pa[0])**2 + (pc[1]-pa[1])**2)
  4492. #
  4493. # # Semiperimeter of triangle
  4494. # s = (a + b + c)/2.0
  4495. #
  4496. # # Area of triangle by Heron's formula
  4497. # area = math.sqrt(s*(s-a)*(s-b)*(s-c))
  4498. # circum_r = a*b*c/(4.0*area)
  4499. #
  4500. # # Here's the radius filter.
  4501. # #print circum_r
  4502. # if circum_r < 1.0/alpha:
  4503. # add_edge(edges, edge_points, coords, ia, ib)
  4504. # add_edge(edges, edge_points, coords, ib, ic)
  4505. # add_edge(edges, edge_points, coords, ic, ia)
  4506. #
  4507. # m = MultiLineString(edge_points)
  4508. # triangles = list(polygonize(m))
  4509. # return cascaded_union(triangles), edge_points
  4510. # def voronoi(P):
  4511. # """
  4512. # Returns a list of all edges of the voronoi diagram for the given input points.
  4513. # """
  4514. # delauny = Delaunay(P)
  4515. # triangles = delauny.points[delauny.vertices]
  4516. #
  4517. # circum_centers = np.array([triangle_csc(tri) for tri in triangles])
  4518. # long_lines_endpoints = []
  4519. #
  4520. # lineIndices = []
  4521. # for i, triangle in enumerate(triangles):
  4522. # circum_center = circum_centers[i]
  4523. # for j, neighbor in enumerate(delauny.neighbors[i]):
  4524. # if neighbor != -1:
  4525. # lineIndices.append((i, neighbor))
  4526. # else:
  4527. # ps = triangle[(j+1)%3] - triangle[(j-1)%3]
  4528. # ps = np.array((ps[1], -ps[0]))
  4529. #
  4530. # middle = (triangle[(j+1)%3] + triangle[(j-1)%3]) * 0.5
  4531. # di = middle - triangle[j]
  4532. #
  4533. # ps /= np.linalg.norm(ps)
  4534. # di /= np.linalg.norm(di)
  4535. #
  4536. # if np.dot(di, ps) < 0.0:
  4537. # ps *= -1000.0
  4538. # else:
  4539. # ps *= 1000.0
  4540. #
  4541. # long_lines_endpoints.append(circum_center + ps)
  4542. # lineIndices.append((i, len(circum_centers) + len(long_lines_endpoints)-1))
  4543. #
  4544. # vertices = np.vstack((circum_centers, long_lines_endpoints))
  4545. #
  4546. # # filter out any duplicate lines
  4547. # lineIndicesSorted = np.sort(lineIndices) # make (1,2) and (2,1) both (1,2)
  4548. # lineIndicesTupled = [tuple(row) for row in lineIndicesSorted]
  4549. # lineIndicesUnique = np.unique(lineIndicesTupled)
  4550. #
  4551. # return vertices, lineIndicesUnique
  4552. #
  4553. #
  4554. # def triangle_csc(pts):
  4555. # rows, cols = pts.shape
  4556. #
  4557. # A = np.bmat([[2 * np.dot(pts, pts.T), np.ones((rows, 1))],
  4558. # [np.ones((1, rows)), np.zeros((1, 1))]])
  4559. #
  4560. # b = np.hstack((np.sum(pts * pts, axis=1), np.ones((1))))
  4561. # x = np.linalg.solve(A,b)
  4562. # bary_coords = x[:-1]
  4563. # return np.sum(pts * np.tile(bary_coords.reshape((pts.shape[0], 1)), (1, pts.shape[1])), axis=0)
  4564. #
  4565. #
  4566. # def voronoi_cell_lines(points, vertices, lineIndices):
  4567. # """
  4568. # Returns a mapping from a voronoi cell to its edges.
  4569. #
  4570. # :param points: shape (m,2)
  4571. # :param vertices: shape (n,2)
  4572. # :param lineIndices: shape (o,2)
  4573. # :rtype: dict point index -> list of shape (n,2) with vertex indices
  4574. # """
  4575. # kd = KDTree(points)
  4576. #
  4577. # cells = collections.defaultdict(list)
  4578. # for i1, i2 in lineIndices:
  4579. # v1, v2 = vertices[i1], vertices[i2]
  4580. # mid = (v1+v2)/2
  4581. # _, (p1Idx, p2Idx) = kd.query(mid, 2)
  4582. # cells[p1Idx].append((i1, i2))
  4583. # cells[p2Idx].append((i1, i2))
  4584. #
  4585. # return cells
  4586. #
  4587. #
  4588. # def voronoi_edges2polygons(cells):
  4589. # """
  4590. # Transforms cell edges into polygons.
  4591. #
  4592. # :param cells: as returned from voronoi_cell_lines
  4593. # :rtype: dict point index -> list of vertex indices which form a polygon
  4594. # """
  4595. #
  4596. # # first, close the outer cells
  4597. # for pIdx, lineIndices_ in cells.items():
  4598. # dangling_lines = []
  4599. # for i1, i2 in lineIndices_:
  4600. # p = (i1, i2)
  4601. # connections = filter(lambda k: p != k and (p[0] == k[0] or p[0] == k[1] or p[1] == k[0] or p[1] == k[1]), lineIndices_)
  4602. # # connections = filter(lambda (i1_, i2_): (i1, i2) != (i1_, i2_) and (i1 == i1_ or i1 == i2_ or i2 == i1_ or i2 == i2_), lineIndices_)
  4603. # assert 1 <= len(connections) <= 2
  4604. # if len(connections) == 1:
  4605. # dangling_lines.append((i1, i2))
  4606. # assert len(dangling_lines) in [0, 2]
  4607. # if len(dangling_lines) == 2:
  4608. # (i11, i12), (i21, i22) = dangling_lines
  4609. # s = (i11, i12)
  4610. # t = (i21, i22)
  4611. #
  4612. # # determine which line ends are unconnected
  4613. # connected = filter(lambda k: k != s and (k[0] == s[0] or k[1] == s[0]), lineIndices_)
  4614. # # connected = filter(lambda (i1,i2): (i1,i2) != (i11,i12) and (i1 == i11 or i2 == i11), lineIndices_)
  4615. # i11Unconnected = len(connected) == 0
  4616. #
  4617. # connected = filter(lambda k: k != t and (k[0] == t[0] or k[1] == t[0]), lineIndices_)
  4618. # # connected = filter(lambda (i1,i2): (i1,i2) != (i21,i22) and (i1 == i21 or i2 == i21), lineIndices_)
  4619. # i21Unconnected = len(connected) == 0
  4620. #
  4621. # startIdx = i11 if i11Unconnected else i12
  4622. # endIdx = i21 if i21Unconnected else i22
  4623. #
  4624. # cells[pIdx].append((startIdx, endIdx))
  4625. #
  4626. # # then, form polygons by storing vertex indices in (counter-)clockwise order
  4627. # polys = dict()
  4628. # for pIdx, lineIndices_ in cells.items():
  4629. # # get a directed graph which contains both directions and arbitrarily follow one of both
  4630. # directedGraph = lineIndices_ + [(i2, i1) for (i1, i2) in lineIndices_]
  4631. # directedGraphMap = collections.defaultdict(list)
  4632. # for (i1, i2) in directedGraph:
  4633. # directedGraphMap[i1].append(i2)
  4634. # orderedEdges = []
  4635. # currentEdge = directedGraph[0]
  4636. # while len(orderedEdges) < len(lineIndices_):
  4637. # i1 = currentEdge[1]
  4638. # i2 = directedGraphMap[i1][0] if directedGraphMap[i1][0] != currentEdge[0] else directedGraphMap[i1][1]
  4639. # nextEdge = (i1, i2)
  4640. # orderedEdges.append(nextEdge)
  4641. # currentEdge = nextEdge
  4642. #
  4643. # polys[pIdx] = [i1 for (i1, i2) in orderedEdges]
  4644. #
  4645. # return polys
  4646. #
  4647. #
  4648. # def voronoi_polygons(points):
  4649. # """
  4650. # Returns the voronoi polygon for each input point.
  4651. #
  4652. # :param points: shape (n,2)
  4653. # :rtype: list of n polygons where each polygon is an array of vertices
  4654. # """
  4655. # vertices, lineIndices = voronoi(points)
  4656. # cells = voronoi_cell_lines(points, vertices, lineIndices)
  4657. # polys = voronoi_edges2polygons(cells)
  4658. # polylist = []
  4659. # for i in range(len(points)):
  4660. # poly = vertices[np.asarray(polys[i])]
  4661. # polylist.append(poly)
  4662. # return polylist
  4663. #
  4664. #
  4665. # class Zprofile:
  4666. # def __init__(self):
  4667. #
  4668. # # data contains lists of [x, y, z]
  4669. # self.data = []
  4670. #
  4671. # # Computed voronoi polygons (shapely)
  4672. # self.polygons = []
  4673. # pass
  4674. #
  4675. # # def plot_polygons(self):
  4676. # # axes = plt.subplot(1, 1, 1)
  4677. # #
  4678. # # plt.axis([-0.05, 1.05, -0.05, 1.05])
  4679. # #
  4680. # # for poly in self.polygons:
  4681. # # p = PolygonPatch(poly, facecolor=np.random.rand(3, 1), alpha=0.3)
  4682. # # axes.add_patch(p)
  4683. #
  4684. # def init_from_csv(self, filename):
  4685. # pass
  4686. #
  4687. # def init_from_string(self, zpstring):
  4688. # pass
  4689. #
  4690. # def init_from_list(self, zplist):
  4691. # self.data = zplist
  4692. #
  4693. # def generate_polygons(self):
  4694. # self.polygons = [Polygon(p) for p in voronoi_polygons(array([[x[0], x[1]] for x in self.data]))]
  4695. #
  4696. # def normalize(self, origin):
  4697. # pass
  4698. #
  4699. # def paste(self, path):
  4700. # """
  4701. # Return a list of dictionaries containing the parts of the original
  4702. # path and their z-axis offset.
  4703. # """
  4704. #
  4705. # # At most one region/polygon will contain the path
  4706. # containing = [i for i in range(len(self.polygons)) if self.polygons[i].contains(path)]
  4707. #
  4708. # if len(containing) > 0:
  4709. # return [{"path": path, "z": self.data[containing[0]][2]}]
  4710. #
  4711. # # All region indexes that intersect with the path
  4712. # crossing = [i for i in range(len(self.polygons)) if self.polygons[i].intersects(path)]
  4713. #
  4714. # return [{"path": path.intersection(self.polygons[i]),
  4715. # "z": self.data[i][2]} for i in crossing]
  4716. def autolist(obj):
  4717. try:
  4718. __ = iter(obj)
  4719. return obj
  4720. except TypeError:
  4721. return [obj]
  4722. def three_point_circle(p1, p2, p3):
  4723. """
  4724. Computes the center and radius of a circle from
  4725. 3 points on its circumference.
  4726. :param p1: Point 1
  4727. :param p2: Point 2
  4728. :param p3: Point 3
  4729. :return: center, radius
  4730. """
  4731. # Midpoints
  4732. a1 = (p1 + p2) / 2.0
  4733. a2 = (p2 + p3) / 2.0
  4734. # Normals
  4735. b1 = dot((p2 - p1), array([[0, -1], [1, 0]], dtype=float32))
  4736. b2 = dot((p3 - p2), array([[0, 1], [-1, 0]], dtype=float32))
  4737. # Params
  4738. try:
  4739. T = solve(transpose(array([-b1, b2])), a1 - a2)
  4740. except Exception as e:
  4741. log.debug("camlib.three_point_circle() --> %s" % str(e))
  4742. return
  4743. # Center
  4744. center = a1 + b1 * T[0]
  4745. # Radius
  4746. radius = np.linalg.norm(center - p1)
  4747. return center, radius, T[0]
  4748. def distance(pt1, pt2):
  4749. return sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2)
  4750. def distance_euclidian(x1, y1, x2, y2):
  4751. return sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
  4752. class FlatCAMRTree(object):
  4753. """
  4754. Indexes geometry (Any object with "cooords" property containing
  4755. a list of tuples with x, y values). Objects are indexed by
  4756. all their points by default. To index by arbitrary points,
  4757. override self.points2obj.
  4758. """
  4759. def __init__(self):
  4760. # Python RTree Index
  4761. self.rti = rtindex.Index()
  4762. # ## Track object-point relationship
  4763. # Each is list of points in object.
  4764. self.obj2points = []
  4765. # Index is index in rtree, value is index of
  4766. # object in obj2points.
  4767. self.points2obj = []
  4768. self.get_points = lambda go: go.coords
  4769. def grow_obj2points(self, idx):
  4770. """
  4771. Increases the size of self.obj2points to fit
  4772. idx + 1 items.
  4773. :param idx: Index to fit into list.
  4774. :return: None
  4775. """
  4776. if len(self.obj2points) > idx:
  4777. # len == 2, idx == 1, ok.
  4778. return
  4779. else:
  4780. # len == 2, idx == 2, need 1 more.
  4781. # range(2, 3)
  4782. for i in range(len(self.obj2points), idx + 1):
  4783. self.obj2points.append([])
  4784. def insert(self, objid, obj):
  4785. self.grow_obj2points(objid)
  4786. self.obj2points[objid] = []
  4787. for pt in self.get_points(obj):
  4788. self.rti.insert(len(self.points2obj), (pt[0], pt[1], pt[0], pt[1]), obj=objid)
  4789. self.obj2points[objid].append(len(self.points2obj))
  4790. self.points2obj.append(objid)
  4791. def remove_obj(self, objid, obj):
  4792. # Use all ptids to delete from index
  4793. for i, pt in enumerate(self.get_points(obj)):
  4794. try:
  4795. self.rti.delete(self.obj2points[objid][i], (pt[0], pt[1], pt[0], pt[1]))
  4796. except IndexError:
  4797. pass
  4798. def nearest(self, pt):
  4799. """
  4800. Will raise StopIteration if no items are found.
  4801. :param pt:
  4802. :return:
  4803. """
  4804. return next(self.rti.nearest(pt, objects=True))
  4805. class FlatCAMRTreeStorage(FlatCAMRTree):
  4806. """
  4807. Just like FlatCAMRTree it indexes geometry, but also serves
  4808. as storage for the geometry.
  4809. """
  4810. def __init__(self):
  4811. # super(FlatCAMRTreeStorage, self).__init__()
  4812. super().__init__()
  4813. self.objects = []
  4814. # Optimization attempt!
  4815. self.indexes = {}
  4816. def insert(self, obj):
  4817. self.objects.append(obj)
  4818. idx = len(self.objects) - 1
  4819. # Note: Shapely objects are not hashable any more, althought
  4820. # there seem to be plans to re-introduce the feature in
  4821. # version 2.0. For now, we will index using the object's id,
  4822. # but it's important to remember that shapely geometry is
  4823. # mutable, ie. it can be modified to a totally different shape
  4824. # and continue to have the same id.
  4825. # self.indexes[obj] = idx
  4826. self.indexes[id(obj)] = idx
  4827. # super(FlatCAMRTreeStorage, self).insert(idx, obj)
  4828. super().insert(idx, obj)
  4829. # @profile
  4830. def remove(self, obj):
  4831. # See note about self.indexes in insert().
  4832. # objidx = self.indexes[obj]
  4833. objidx = self.indexes[id(obj)]
  4834. # Remove from list
  4835. self.objects[objidx] = None
  4836. # Remove from index
  4837. self.remove_obj(objidx, obj)
  4838. def get_objects(self):
  4839. return (o for o in self.objects if o is not None)
  4840. def nearest(self, pt):
  4841. """
  4842. Returns the nearest matching points and the object
  4843. it belongs to.
  4844. :param pt: Query point.
  4845. :return: (match_x, match_y), Object owner of
  4846. matching point.
  4847. :rtype: tuple
  4848. """
  4849. tidx = super(FlatCAMRTreeStorage, self).nearest(pt)
  4850. return (tidx.bbox[0], tidx.bbox[1]), self.objects[tidx.object]
  4851. # class myO:
  4852. # def __init__(self, coords):
  4853. # self.coords = coords
  4854. #
  4855. #
  4856. # def test_rti():
  4857. #
  4858. # o1 = myO([(0, 0), (0, 1), (1, 1)])
  4859. # o2 = myO([(2, 0), (2, 1), (2, 1)])
  4860. # o3 = myO([(2, 0), (2, 1), (3, 1)])
  4861. #
  4862. # os = [o1, o2]
  4863. #
  4864. # idx = FlatCAMRTree()
  4865. #
  4866. # for o in range(len(os)):
  4867. # idx.insert(o, os[o])
  4868. #
  4869. # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]
  4870. #
  4871. # idx.remove_obj(0, o1)
  4872. #
  4873. # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]
  4874. #
  4875. # idx.remove_obj(1, o2)
  4876. #
  4877. # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]
  4878. #
  4879. #
  4880. # def test_rtis():
  4881. #
  4882. # o1 = myO([(0, 0), (0, 1), (1, 1)])
  4883. # o2 = myO([(2, 0), (2, 1), (2, 1)])
  4884. # o3 = myO([(2, 0), (2, 1), (3, 1)])
  4885. #
  4886. # os = [o1, o2]
  4887. #
  4888. # idx = FlatCAMRTreeStorage()
  4889. #
  4890. # for o in range(len(os)):
  4891. # idx.insert(os[o])
  4892. #
  4893. # #os = None
  4894. # #o1 = None
  4895. # #o2 = None
  4896. #
  4897. # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]
  4898. #
  4899. # idx.remove(idx.nearest((2,0))[1])
  4900. #
  4901. # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]
  4902. #
  4903. # idx.remove(idx.nearest((0,0))[1])
  4904. #
  4905. # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]