| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923 |
- ############################################################
- # FlatCAM: 2D Post-processing for Manufacturing #
- # http://flatcam.org #
- # Author: Juan Pablo Caram (c) #
- # Date: 2/5/2014 #
- # MIT Licence #
- ############################################################
- #from __future__ import division
- #from scipy import optimize
- #import traceback
- from numpy import arctan2, Inf, array, sqrt, pi, ceil, sin, cos, dot, float32, \
- transpose
- from numpy.linalg import solve, norm
- from matplotlib.figure import Figure
- import re
- import sys
- import traceback
- from decimal import Decimal
- import collections
- import numpy as np
- import matplotlib
- #import matplotlib.pyplot as plt
- #from scipy.spatial import Delaunay, KDTree
- from rtree import index as rtindex
- # See: http://toblerity.org/shapely/manual.html
- from shapely.geometry import Polygon, LineString, Point, LinearRing
- from shapely.geometry import MultiPoint, MultiPolygon
- from shapely.geometry import box as shply_box
- from shapely.ops import cascaded_union
- import shapely.affinity as affinity
- from shapely.wkt import loads as sloads
- from shapely.wkt import dumps as sdumps
- from shapely.geometry.base import BaseGeometry
- # Used for solid polygons in Matplotlib
- from descartes.patch import PolygonPatch
- import simplejson as json
- # TODO: Commented for FlatCAM packaging with cx_freeze
- #from matplotlib.pyplot import plot, subplot
- import xml.etree.ElementTree as ET
- from svg.path import Path, Line, Arc, CubicBezier, QuadraticBezier, parse_path
- import itertools
- import xml.etree.ElementTree as ET
- from svg.path import Path, Line, Arc, CubicBezier, QuadraticBezier, parse_path
- from svgparse import *
- import logging
- log = logging.getLogger('base2')
- log.setLevel(logging.DEBUG)
- # log.setLevel(logging.WARNING)
- # log.setLevel(logging.INFO)
- formatter = logging.Formatter('[%(levelname)s] %(message)s')
- handler = logging.StreamHandler()
- handler.setFormatter(formatter)
- log.addHandler(handler)
- class ParseError(Exception):
- pass
- class Geometry(object):
- """
- Base geometry class.
- """
- defaults = {
- "init_units": 'in'
- }
- def __init__(self):
- # Units (in or mm)
- self.units = Geometry.defaults["init_units"]
-
- # Final geometry: MultiPolygon or list (of geometry constructs)
- self.solid_geometry = None
- # Attributes to be included in serialization
- self.ser_attrs = ['units', 'solid_geometry']
- # Flattened geometry (list of paths only)
- self.flat_geometry = []
- def add_circle(self, origin, radius):
- """
- Adds a circle to the object.
- :param origin: Center of the circle.
- :param radius: Radius of the circle.
- :return: None
- """
- # TODO: Decide what solid_geometry is supposed to be and how we append to it.
- if self.solid_geometry is None:
- self.solid_geometry = []
- if type(self.solid_geometry) is list:
- self.solid_geometry.append(Point(origin).buffer(radius))
- return
- try:
- self.solid_geometry = self.solid_geometry.union(Point(origin).buffer(radius))
- except:
- #print "Failed to run union on polygons."
- log.error("Failed to run union on polygons.")
- raise
- def add_polygon(self, points):
- """
- Adds a polygon to the object (by union)
- :param points: The vertices of the polygon.
- :return: None
- """
- if self.solid_geometry is None:
- self.solid_geometry = []
- if type(self.solid_geometry) is list:
- self.solid_geometry.append(Polygon(points))
- return
- try:
- self.solid_geometry = self.solid_geometry.union(Polygon(points))
- except:
- #print "Failed to run union on polygons."
- log.error("Failed to run union on polygons.")
- raise
- def bounds(self):
- """
- Returns coordinates of rectangular bounds
- of geometry: (xmin, ymin, xmax, ymax).
- """
- log.debug("Geometry->bounds()")
- if self.solid_geometry is None:
- log.debug("solid_geometry is None")
- return 0, 0, 0, 0
- if type(self.solid_geometry) is list:
- # TODO: This can be done faster. See comment from Shapely mailing lists.
- if len(self.solid_geometry) == 0:
- log.debug('solid_geometry is empty []')
- return 0, 0, 0, 0
- return cascaded_union(self.solid_geometry).bounds
- else:
- return self.solid_geometry.bounds
- def find_polygon(self, point, geoset=None):
- """
- Find an object that object.contains(Point(point)) in
- poly, which can can be iterable, contain iterable of, or
- be itself an implementer of .contains().
- :param poly: See description
- :return: Polygon containing point or None.
- """
- if geoset is None:
- geoset = self.solid_geometry
- try: # Iterable
- for sub_geo in geoset:
- p = self.find_polygon(point, geoset=sub_geo)
- if p is not None:
- return p
- except TypeError: # Non-iterable
- try: # Implements .contains()
- if geoset.contains(Point(point)):
- return geoset
- except AttributeError: # Does not implement .contains()
- return None
- return None
- def get_interiors(self, geometry=None):
- interiors = []
- if geometry is None:
- geometry = self.solid_geometry
- ## If iterable, expand recursively.
- try:
- for geo in geometry:
- interiors.extend(self.get_interiors(geometry=geo))
- ## Not iterable, get the exterior if polygon.
- except TypeError:
- if type(geometry) == Polygon:
- interiors.extend(geometry.interiors)
- return interiors
- def get_exteriors(self, geometry=None):
- """
- Returns all exteriors of polygons in geometry. Uses
- ``self.solid_geometry`` if geometry is not provided.
- :param geometry: Shapely type or list or list of list of such.
- :return: List of paths constituting the exteriors
- of polygons in geometry.
- """
- exteriors = []
- if geometry is None:
- geometry = self.solid_geometry
- ## If iterable, expand recursively.
- try:
- for geo in geometry:
- exteriors.extend(self.get_exteriors(geometry=geo))
- ## Not iterable, get the exterior if polygon.
- except TypeError:
- if type(geometry) == Polygon:
- exteriors.append(geometry.exterior)
- return exteriors
- def flatten(self, geometry=None, reset=True, pathonly=False):
- """
- Creates a list of non-iterable linear geometry objects.
- Polygons are expanded into its exterior and interiors if specified.
- Results are placed in self.flat_geoemtry
- :param geometry: Shapely type or list or list of list of such.
- :param reset: Clears the contents of self.flat_geometry.
- :param pathonly: Expands polygons into linear elements.
- """
- if geometry is None:
- geometry = self.solid_geometry
- if reset:
- self.flat_geometry = []
- ## If iterable, expand recursively.
- try:
- for geo in geometry:
- self.flatten(geometry=geo,
- reset=False,
- pathonly=pathonly)
- ## Not iterable, do the actual indexing and add.
- except TypeError:
- if pathonly and type(geometry) == Polygon:
- self.flat_geometry.append(geometry.exterior)
- self.flatten(geometry=geometry.interiors,
- reset=False,
- pathonly=True)
- else:
- self.flat_geometry.append(geometry)
- return self.flat_geometry
- # def make2Dstorage(self):
- #
- # self.flatten()
- #
- # def get_pts(o):
- # pts = []
- # if type(o) == Polygon:
- # g = o.exterior
- # pts += list(g.coords)
- # for i in o.interiors:
- # pts += list(i.coords)
- # else:
- # pts += list(o.coords)
- # return pts
- #
- # storage = FlatCAMRTreeStorage()
- # storage.get_points = get_pts
- # for shape in self.flat_geometry:
- # storage.insert(shape)
- # return storage
- # def flatten_to_paths(self, geometry=None, reset=True):
- # """
- # Creates a list of non-iterable linear geometry elements and
- # indexes them in rtree.
- #
- # :param geometry: Iterable geometry
- # :param reset: Wether to clear (True) or append (False) to self.flat_geometry
- # :return: self.flat_geometry, self.flat_geometry_rtree
- # """
- #
- # if geometry is None:
- # geometry = self.solid_geometry
- #
- # if reset:
- # self.flat_geometry = []
- #
- # ## If iterable, expand recursively.
- # try:
- # for geo in geometry:
- # self.flatten_to_paths(geometry=geo, reset=False)
- #
- # ## Not iterable, do the actual indexing and add.
- # except TypeError:
- # if type(geometry) == Polygon:
- # g = geometry.exterior
- # self.flat_geometry.append(g)
- #
- # ## Add first and last points of the path to the index.
- # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[0])
- # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[-1])
- #
- # for interior in geometry.interiors:
- # g = interior
- # self.flat_geometry.append(g)
- # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[0])
- # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[-1])
- # else:
- # g = geometry
- # self.flat_geometry.append(g)
- # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[0])
- # self.flat_geometry_rtree.insert(len(self.flat_geometry) - 1, g.coords[-1])
- #
- # return self.flat_geometry, self.flat_geometry_rtree
- def isolation_geometry(self, offset):
- """
- Creates contours around geometry at a given
- offset distance.
- :param offset: Offset distance.
- :type offset: float
- :return: The buffered geometry.
- :rtype: Shapely.MultiPolygon or Shapely.Polygon
- """
- return self.solid_geometry.buffer(offset)
- def is_empty(self):
- if self.solid_geometry is None:
- return True
- if type(self.solid_geometry) is list and len(self.solid_geometry) == 0:
- return True
- return False
- def import_svg(self, filename, flip=True):
- """
- Imports shapes from an SVG file into the object's geometry.
- :param filename: Path to the SVG file.
- :type filename: str
- :return: None
- """
- # Parse into list of shapely objects
- svg_tree = ET.parse(filename)
- svg_root = svg_tree.getroot()
- # Change origin to bottom left
- # h = float(svg_root.get('height'))
- # w = float(svg_root.get('width'))
- h = svgparselength(svg_root.get('height'))[0] # TODO: No units support yet
- geos = getsvggeo(svg_root)
- if flip:
- geos = [translate(scale(g, 1.0, -1.0, origin=(0, 0)), yoff=h) for g in geos]
- # Add to object
- if self.solid_geometry is None:
- self.solid_geometry = []
- if type(self.solid_geometry) is list:
- self.solid_geometry.append(cascaded_union(geos))
- else: # It's shapely geometry
- self.solid_geometry = cascaded_union([self.solid_geometry,
- cascaded_union(geos)])
- return
- def size(self):
- """
- Returns (width, height) of rectangular
- bounds of geometry.
- """
- if self.solid_geometry is None:
- log.warning("Solid_geometry not computed yet.")
- return 0
- bounds = self.bounds()
- return bounds[2] - bounds[0], bounds[3] - bounds[1]
-
- def get_empty_area(self, boundary=None):
- """
- Returns the complement of self.solid_geometry within
- the given boundary polygon. If not specified, it defaults to
- the rectangular bounding box of self.solid_geometry.
- """
- if boundary is None:
- boundary = self.solid_geometry.envelope
- return boundary.difference(self.solid_geometry)
-
- @staticmethod
- def clear_polygon(polygon, tooldia, overlap=0.15):
- """
- Creates geometry inside a polygon for a tool to cover
- the whole area.
- This algorithm shrinks the edges of the polygon and takes
- the resulting edges as toolpaths.
- :param polygon: Polygon to clear.
- :param tooldia: Diameter of the tool.
- :param overlap: Overlap of toolpasses.
- :return:
- """
- log.debug("camlib.clear_polygon()")
- assert type(polygon) == Polygon or type(polygon) == MultiPolygon, \
- "Expected a Polygon or MultiPolygon, got %s" % type(polygon)
- ## The toolpaths
- # Index first and last points in paths
- def get_pts(o):
- return [o.coords[0], o.coords[-1]]
- geoms = FlatCAMRTreeStorage()
- geoms.get_points = get_pts
- # Can only result in a Polygon or MultiPolygon
- current = polygon.buffer(-tooldia / 2.0)
- # current can be a MultiPolygon
- try:
- for p in current:
- geoms.insert(p.exterior)
- for i in p.interiors:
- geoms.insert(i)
- # Not a Multipolygon. Must be a Polygon
- except TypeError:
- geoms.insert(current.exterior)
- for i in current.interiors:
- geoms.insert(i)
- while True:
- # Can only result in a Polygon or MultiPolygon
- current = current.buffer(-tooldia * (1 - overlap))
- if current.area > 0:
- # current can be a MultiPolygon
- try:
- for p in current:
- geoms.insert(p.exterior)
- for i in p.interiors:
- geoms.insert(i)
- # Not a Multipolygon. Must be a Polygon
- except TypeError:
- geoms.insert(current.exterior)
- for i in current.interiors:
- geoms.insert(i)
- else:
- break
- # Optimization: Reduce lifts
- log.debug("Reducing tool lifts...")
- geoms = Geometry.paint_connect(geoms, polygon, tooldia)
- return geoms
- @staticmethod
- def clear_polygon2(polygon, tooldia, seedpoint=None, overlap=0.15):
- """
- Creates geometry inside a polygon for a tool to cover
- the whole area.
- This algorithm starts with a seed point inside the polygon
- and draws circles around it. Arcs inside the polygons are
- valid cuts. Finalizes by cutting around the inside edge of
- the polygon.
- :param polygon: Shapely.geometry.Polygon
- :param tooldia: Diameter of the tool
- :param seedpoint: Shapely.geometry.Point or None
- :param overlap: Tool fraction overlap bewteen passes
- :return: List of toolpaths covering polygon.
- """
- log.debug("camlib.clear_polygon2()")
- # Current buffer radius
- radius = tooldia / 2 * (1 - overlap)
- ## The toolpaths
- # Index first and last points in paths
- def get_pts(o):
- return [o.coords[0], o.coords[-1]]
- geoms = FlatCAMRTreeStorage()
- geoms.get_points = get_pts
- # Path margin
- path_margin = polygon.buffer(-tooldia / 2)
- # Estimate good seedpoint if not provided.
- if seedpoint is None:
- seedpoint = path_margin.representative_point()
- # Grow from seed until outside the box. The polygons will
- # never have an interior, so take the exterior LinearRing.
- while 1:
- path = Point(seedpoint).buffer(radius).exterior
- path = path.intersection(path_margin)
- # Touches polygon?
- if path.is_empty:
- break
- else:
- #geoms.append(path)
- #geoms.insert(path)
- # path can be a collection of paths.
- try:
- for p in path:
- geoms.insert(p)
- except TypeError:
- geoms.insert(path)
- radius += tooldia * (1 - overlap)
- # Clean inside edges of the original polygon
- outer_edges = [x.exterior for x in autolist(polygon.buffer(-tooldia / 2))]
- inner_edges = []
- for x in autolist(polygon.buffer(-tooldia / 2)): # Over resulting polygons
- for y in x.interiors: # Over interiors of each polygon
- inner_edges.append(y)
- #geoms += outer_edges + inner_edges
- for g in outer_edges + inner_edges:
- geoms.insert(g)
- # Optimization connect touching paths
- # log.debug("Connecting paths...")
- # geoms = Geometry.path_connect(geoms)
- # Optimization: Reduce lifts
- log.debug("Reducing tool lifts...")
- geoms = Geometry.paint_connect(geoms, polygon, tooldia)
- return geoms
- def scale(self, factor):
- """
- Scales all of the object's geometry by a given factor. Override
- this method.
- :param factor: Number by which to scale.
- :type factor: float
- :return: None
- :rtype: None
- """
- return
- def offset(self, vect):
- """
- Offset the geometry by the given vector. Override this method.
- :param vect: (x, y) vector by which to offset the object.
- :type vect: tuple
- :return: None
- """
- return
- @staticmethod
- def paint_connect(storage, boundary, tooldia, max_walk=None):
- """
- Connects paths that results in a connection segment that is
- within the paint area. This avoids unnecessary tool lifting.
- :param storage: Geometry to be optimized.
- :type storage: FlatCAMRTreeStorage
- :param boundary: Polygon defining the limits of the paintable area.
- :type boundary: Polygon
- :param max_walk: Maximum allowable distance without lifting tool.
- :type max_walk: float or None
- :return: Optimized geometry.
- :rtype: FlatCAMRTreeStorage
- """
- # If max_walk is not specified, the maximum allowed is
- # 10 times the tool diameter
- max_walk = max_walk or 10 * tooldia
- # Assuming geolist is a flat list of flat elements
- ## Index first and last points in paths
- def get_pts(o):
- return [o.coords[0], o.coords[-1]]
- # storage = FlatCAMRTreeStorage()
- # storage.get_points = get_pts
- #
- # for shape in geolist:
- # if shape is not None: # TODO: This shouldn't have happened.
- # # Make LlinearRings into linestrings otherwise
- # # When chaining the coordinates path is messed up.
- # storage.insert(LineString(shape))
- # #storage.insert(shape)
- ## Iterate over geometry paths getting the nearest each time.
- #optimized_paths = []
- optimized_paths = FlatCAMRTreeStorage()
- optimized_paths.get_points = get_pts
- path_count = 0
- current_pt = (0, 0)
- pt, geo = storage.nearest(current_pt)
- storage.remove(geo)
- geo = LineString(geo)
- current_pt = geo.coords[-1]
- try:
- while True:
- path_count += 1
- #log.debug("Path %d" % path_count)
- pt, candidate = storage.nearest(current_pt)
- storage.remove(candidate)
- candidate = LineString(candidate)
- # If last point in geometry is the nearest
- # then reverse coordinates.
- # but prefer the first one if last == first
- if pt != candidate.coords[0] and pt == candidate.coords[-1]:
- candidate.coords = list(candidate.coords)[::-1]
- # Straight line from current_pt to pt.
- # Is the toolpath inside the geometry?
- walk_path = LineString([current_pt, pt])
- walk_cut = walk_path.buffer(tooldia / 2)
- if walk_cut.within(boundary) and walk_path.length < max_walk:
- #log.debug("Walk to path #%d is inside. Joining." % path_count)
- # Completely inside. Append...
- geo.coords = list(geo.coords) + list(candidate.coords)
- # try:
- # last = optimized_paths[-1]
- # last.coords = list(last.coords) + list(geo.coords)
- # except IndexError:
- # optimized_paths.append(geo)
- else:
- # Have to lift tool. End path.
- #log.debug("Path #%d not within boundary. Next." % path_count)
- #optimized_paths.append(geo)
- optimized_paths.insert(geo)
- geo = candidate
- current_pt = geo.coords[-1]
- # Next
- #pt, geo = storage.nearest(current_pt)
- except StopIteration: # Nothing left in storage.
- #pass
- optimized_paths.insert(geo)
- return optimized_paths
- @staticmethod
- def path_connect(storage, origin=(0, 0)):
- """
- :return: None
- """
- log.debug("path_connect()")
- ## Index first and last points in paths
- def get_pts(o):
- return [o.coords[0], o.coords[-1]]
- #
- # storage = FlatCAMRTreeStorage()
- # storage.get_points = get_pts
- #
- # for shape in pathlist:
- # if shape is not None: # TODO: This shouldn't have happened.
- # storage.insert(shape)
- path_count = 0
- pt, geo = storage.nearest(origin)
- storage.remove(geo)
- #optimized_geometry = [geo]
- optimized_geometry = FlatCAMRTreeStorage()
- optimized_geometry.get_points = get_pts
- #optimized_geometry.insert(geo)
- try:
- while True:
- path_count += 1
- #print "geo is", geo
- _, left = storage.nearest(geo.coords[0])
- #print "left is", left
- # If left touches geo, remove left from original
- # storage and append to geo.
- if type(left) == LineString:
- if left.coords[0] == geo.coords[0]:
- storage.remove(left)
- geo.coords = list(geo.coords)[::-1] + list(left.coords)
- continue
- if left.coords[-1] == geo.coords[0]:
- storage.remove(left)
- geo.coords = list(left.coords) + list(geo.coords)
- continue
- if left.coords[0] == geo.coords[-1]:
- storage.remove(left)
- geo.coords = list(geo.coords) + list(left.coords)
- continue
- if left.coords[-1] == geo.coords[-1]:
- storage.remove(left)
- geo.coords = list(geo.coords) + list(left.coords)[::-1]
- continue
- _, right = storage.nearest(geo.coords[-1])
- #print "right is", right
- # If right touches geo, remove left from original
- # storage and append to geo.
- if type(right) == LineString:
- if right.coords[0] == geo.coords[-1]:
- storage.remove(right)
- geo.coords = list(geo.coords) + list(right.coords)
- continue
- if right.coords[-1] == geo.coords[-1]:
- storage.remove(right)
- geo.coords = list(geo.coords) + list(right.coords)[::-1]
- continue
- if right.coords[0] == geo.coords[0]:
- storage.remove(right)
- geo.coords = list(geo.coords)[::-1] + list(right.coords)
- continue
- if right.coords[-1] == geo.coords[0]:
- storage.remove(right)
- geo.coords = list(left.coords) + list(geo.coords)
- continue
- # right is either a LinearRing or it does not connect
- # to geo (nothing left to connect to geo), so we continue
- # with right as geo.
- storage.remove(right)
- if type(right) == LinearRing:
- optimized_geometry.insert(right)
- else:
- # Cannot exteng geo any further. Put it away.
- optimized_geometry.insert(geo)
- # Continue with right.
- geo = right
- except StopIteration: # Nothing found in storage.
- optimized_geometry.insert(geo)
- #print path_count
- log.debug("path_count = %d" % path_count)
- return optimized_geometry
- def convert_units(self, units):
- """
- Converts the units of the object to ``units`` by scaling all
- the geometry appropriately. This call ``scale()``. Don't call
- it again in descendents.
- :param units: "IN" or "MM"
- :type units: str
- :return: Scaling factor resulting from unit change.
- :rtype: float
- """
- log.debug("Geometry.convert_units()")
- if units.upper() == self.units.upper():
- return 1.0
- if units.upper() == "MM":
- factor = 25.4
- elif units.upper() == "IN":
- factor = 1 / 25.4
- else:
- log.error("Unsupported units: %s" % str(units))
- return 1.0
- self.units = units
- self.scale(factor)
- return factor
- def to_dict(self):
- """
- Returns a respresentation of the object as a dictionary.
- Attributes to include are listed in ``self.ser_attrs``.
- :return: A dictionary-encoded copy of the object.
- :rtype: dict
- """
- d = {}
- for attr in self.ser_attrs:
- d[attr] = getattr(self, attr)
- return d
- def from_dict(self, d):
- """
- Sets object's attributes from a dictionary.
- Attributes to include are listed in ``self.ser_attrs``.
- This method will look only for only and all the
- attributes in ``self.ser_attrs``. They must all
- be present. Use only for deserializing saved
- objects.
- :param d: Dictionary of attributes to set in the object.
- :type d: dict
- :return: None
- """
- for attr in self.ser_attrs:
- setattr(self, attr, d[attr])
- def union(self):
- """
- Runs a cascaded union on the list of objects in
- solid_geometry.
- :return: None
- """
- self.solid_geometry = [cascaded_union(self.solid_geometry)]
- class ApertureMacro:
- """
- Syntax of aperture macros.
- <AM command>: AM<Aperture macro name>*<Macro content>
- <Macro content>: {{<Variable definition>*}{<Primitive>*}}
- <Variable definition>: $K=<Arithmetic expression>
- <Primitive>: <Primitive code>,<Modifier>{,<Modifier>}|<Comment>
- <Modifier>: $M|< Arithmetic expression>
- <Comment>: 0 <Text>
- """
- ## Regular expressions
- am1_re = re.compile(r'^%AM([^\*]+)\*(.+)?(%)?$')
- am2_re = re.compile(r'(.*)%$')
- amcomm_re = re.compile(r'^0(.*)')
- amprim_re = re.compile(r'^[1-9].*')
- amvar_re = re.compile(r'^\$([0-9a-zA-z]+)=(.*)')
- def __init__(self, name=None):
- self.name = name
- self.raw = ""
- ## These below are recomputed for every aperture
- ## definition, in other words, are temporary variables.
- self.primitives = []
- self.locvars = {}
- self.geometry = None
- def to_dict(self):
- """
- Returns the object in a serializable form. Only the name and
- raw are required.
- :return: Dictionary representing the object. JSON ready.
- :rtype: dict
- """
- return {
- 'name': self.name,
- 'raw': self.raw
- }
- def from_dict(self, d):
- """
- Populates the object from a serial representation created
- with ``self.to_dict()``.
- :param d: Serial representation of an ApertureMacro object.
- :return: None
- """
- for attr in ['name', 'raw']:
- setattr(self, attr, d[attr])
- def parse_content(self):
- """
- Creates numerical lists for all primitives in the aperture
- macro (in ``self.raw``) by replacing all variables by their
- values iteratively and evaluating expressions. Results
- are stored in ``self.primitives``.
- :return: None
- """
- # Cleanup
- self.raw = self.raw.replace('\n', '').replace('\r', '').strip(" *")
- self.primitives = []
- # Separate parts
- parts = self.raw.split('*')
- #### Every part in the macro ####
- for part in parts:
- ### Comments. Ignored.
- match = ApertureMacro.amcomm_re.search(part)
- if match:
- continue
- ### Variables
- # These are variables defined locally inside the macro. They can be
- # numerical constant or defind in terms of previously define
- # variables, which can be defined locally or in an aperture
- # definition. All replacements ocurr here.
- match = ApertureMacro.amvar_re.search(part)
- if match:
- var = match.group(1)
- val = match.group(2)
- # Replace variables in value
- for v in self.locvars:
- val = re.sub(r'\$'+str(v)+r'(?![0-9a-zA-Z])', str(self.locvars[v]), val)
- # Make all others 0
- val = re.sub(r'\$[0-9a-zA-Z](?![0-9a-zA-Z])', "0", val)
- # Change x with *
- val = re.sub(r'[xX]', "*", val)
- # Eval() and store.
- self.locvars[var] = eval(val)
- continue
- ### Primitives
- # Each is an array. The first identifies the primitive, while the
- # rest depend on the primitive. All are strings representing a
- # number and may contain variable definition. The values of these
- # variables are defined in an aperture definition.
- match = ApertureMacro.amprim_re.search(part)
- if match:
- ## Replace all variables
- for v in self.locvars:
- part = re.sub(r'\$' + str(v) + r'(?![0-9a-zA-Z])', str(self.locvars[v]), part)
- # Make all others 0
- part = re.sub(r'\$[0-9a-zA-Z](?![0-9a-zA-Z])', "0", part)
- # Change x with *
- part = re.sub(r'[xX]', "*", part)
- ## Store
- elements = part.split(",")
- self.primitives.append([eval(x) for x in elements])
- continue
- log.warning("Unknown syntax of aperture macro part: %s" % str(part))
- def append(self, data):
- """
- Appends a string to the raw macro.
- :param data: Part of the macro.
- :type data: str
- :return: None
- """
- self.raw += data
- @staticmethod
- def default2zero(n, mods):
- """
- Pads the ``mods`` list with zeros resulting in an
- list of length n.
- :param n: Length of the resulting list.
- :type n: int
- :param mods: List to be padded.
- :type mods: list
- :return: Zero-padded list.
- :rtype: list
- """
- x = [0.0] * n
- na = len(mods)
- x[0:na] = mods
- return x
- @staticmethod
- def make_circle(mods):
- """
- :param mods: (Exposure 0/1, Diameter >=0, X-coord, Y-coord)
- :return:
- """
- pol, dia, x, y = ApertureMacro.default2zero(4, mods)
- return {"pol": int(pol), "geometry": Point(x, y).buffer(dia/2)}
- @staticmethod
- def make_vectorline(mods):
- """
- :param mods: (Exposure 0/1, Line width >= 0, X-start, Y-start, X-end, Y-end,
- rotation angle around origin in degrees)
- :return:
- """
- pol, width, xs, ys, xe, ye, angle = ApertureMacro.default2zero(7, mods)
- line = LineString([(xs, ys), (xe, ye)])
- box = line.buffer(width/2, cap_style=2)
- box_rotated = affinity.rotate(box, angle, origin=(0, 0))
- return {"pol": int(pol), "geometry": box_rotated}
- @staticmethod
- def make_centerline(mods):
- """
- :param mods: (Exposure 0/1, width >=0, height >=0, x-center, y-center,
- rotation angle around origin in degrees)
- :return:
- """
- pol, width, height, x, y, angle = ApertureMacro.default2zero(6, mods)
- box = shply_box(x-width/2, y-height/2, x+width/2, y+height/2)
- box_rotated = affinity.rotate(box, angle, origin=(0, 0))
- return {"pol": int(pol), "geometry": box_rotated}
- @staticmethod
- def make_lowerleftline(mods):
- """
- :param mods: (exposure 0/1, width >=0, height >=0, x-lowerleft, y-lowerleft,
- rotation angle around origin in degrees)
- :return:
- """
- pol, width, height, x, y, angle = ApertureMacro.default2zero(6, mods)
- box = shply_box(x, y, x+width, y+height)
- box_rotated = affinity.rotate(box, angle, origin=(0, 0))
- return {"pol": int(pol), "geometry": box_rotated}
- @staticmethod
- def make_outline(mods):
- """
- :param mods:
- :return:
- """
- pol = mods[0]
- n = mods[1]
- points = [(0, 0)]*(n+1)
- for i in range(n+1):
- points[i] = mods[2*i + 2:2*i + 4]
- angle = mods[2*n + 4]
- poly = Polygon(points)
- poly_rotated = affinity.rotate(poly, angle, origin=(0, 0))
- return {"pol": int(pol), "geometry": poly_rotated}
- @staticmethod
- def make_polygon(mods):
- """
- Note: Specs indicate that rotation is only allowed if the center
- (x, y) == (0, 0). I will tolerate breaking this rule.
- :param mods: (exposure 0/1, n_verts 3<=n<=12, x-center, y-center,
- diameter of circumscribed circle >=0, rotation angle around origin)
- :return:
- """
- pol, nverts, x, y, dia, angle = ApertureMacro.default2zero(6, mods)
- points = [(0, 0)]*nverts
- for i in range(nverts):
- points[i] = (x + 0.5 * dia * cos(2*pi * i/nverts),
- y + 0.5 * dia * sin(2*pi * i/nverts))
- poly = Polygon(points)
- poly_rotated = affinity.rotate(poly, angle, origin=(0, 0))
- return {"pol": int(pol), "geometry": poly_rotated}
- @staticmethod
- def make_moire(mods):
- """
- Note: Specs indicate that rotation is only allowed if the center
- (x, y) == (0, 0). I will tolerate breaking this rule.
- :param mods: (x-center, y-center, outer_dia_outer_ring, ring thickness,
- gap, max_rings, crosshair_thickness, crosshair_len, rotation
- angle around origin in degrees)
- :return:
- """
- x, y, dia, thickness, gap, nrings, cross_th, cross_len, angle = ApertureMacro.default2zero(9, mods)
- r = dia/2 - thickness/2
- result = Point((x, y)).buffer(r).exterior.buffer(thickness/2.0)
- ring = Point((x, y)).buffer(r).exterior.buffer(thickness/2.0) # Need a copy!
- i = 1 # Number of rings created so far
- ## If the ring does not have an interior it means that it is
- ## a disk. Then stop.
- while len(ring.interiors) > 0 and i < nrings:
- r -= thickness + gap
- if r <= 0:
- break
- ring = Point((x, y)).buffer(r).exterior.buffer(thickness/2.0)
- result = cascaded_union([result, ring])
- i += 1
- ## Crosshair
- hor = LineString([(x - cross_len, y), (x + cross_len, y)]).buffer(cross_th/2.0, cap_style=2)
- ver = LineString([(x, y-cross_len), (x, y + cross_len)]).buffer(cross_th/2.0, cap_style=2)
- result = cascaded_union([result, hor, ver])
- return {"pol": 1, "geometry": result}
- @staticmethod
- def make_thermal(mods):
- """
- Note: Specs indicate that rotation is only allowed if the center
- (x, y) == (0, 0). I will tolerate breaking this rule.
- :param mods: [x-center, y-center, diameter-outside, diameter-inside,
- gap-thickness, rotation angle around origin]
- :return:
- """
- x, y, dout, din, t, angle = ApertureMacro.default2zero(6, mods)
- ring = Point((x, y)).buffer(dout/2.0).difference(Point((x, y)).buffer(din/2.0))
- hline = LineString([(x - dout/2.0, y), (x + dout/2.0, y)]).buffer(t/2.0, cap_style=3)
- vline = LineString([(x, y - dout/2.0), (x, y + dout/2.0)]).buffer(t/2.0, cap_style=3)
- thermal = ring.difference(hline.union(vline))
- return {"pol": 1, "geometry": thermal}
- def make_geometry(self, modifiers):
- """
- Runs the macro for the given modifiers and generates
- the corresponding geometry.
- :param modifiers: Modifiers (parameters) for this macro
- :type modifiers: list
- :return: Shapely geometry
- :rtype: shapely.geometry.polygon
- """
- ## Primitive makers
- makers = {
- "1": ApertureMacro.make_circle,
- "2": ApertureMacro.make_vectorline,
- "20": ApertureMacro.make_vectorline,
- "21": ApertureMacro.make_centerline,
- "22": ApertureMacro.make_lowerleftline,
- "4": ApertureMacro.make_outline,
- "5": ApertureMacro.make_polygon,
- "6": ApertureMacro.make_moire,
- "7": ApertureMacro.make_thermal
- }
- ## Store modifiers as local variables
- modifiers = modifiers or []
- modifiers = [float(m) for m in modifiers]
- self.locvars = {}
- for i in range(0, len(modifiers)):
- self.locvars[str(i + 1)] = modifiers[i]
- ## Parse
- self.primitives = [] # Cleanup
- self.geometry = Polygon()
- self.parse_content()
- ## Make the geometry
- for primitive in self.primitives:
- # Make the primitive
- prim_geo = makers[str(int(primitive[0]))](primitive[1:])
- # Add it (according to polarity)
- # if self.geometry is None and prim_geo['pol'] == 1:
- # self.geometry = prim_geo['geometry']
- # continue
- if prim_geo['pol'] == 1:
- self.geometry = self.geometry.union(prim_geo['geometry'])
- continue
- if prim_geo['pol'] == 0:
- self.geometry = self.geometry.difference(prim_geo['geometry'])
- continue
- return self.geometry
- class Gerber (Geometry):
- """
- **ATTRIBUTES**
- * ``apertures`` (dict): The keys are names/identifiers of each aperture.
- The values are dictionaries key/value pairs which describe the aperture. The
- type key is always present and the rest depend on the key:
- +-----------+-----------------------------------+
- | Key | Value |
- +===========+===================================+
- | type | (str) "C", "R", "O", "P", or "AP" |
- +-----------+-----------------------------------+
- | others | Depend on ``type`` |
- +-----------+-----------------------------------+
- * ``aperture_macros`` (dictionary): Are predefined geometrical structures
- that can be instanciated with different parameters in an aperture
- definition. See ``apertures`` above. The key is the name of the macro,
- and the macro itself, the value, is a ``Aperture_Macro`` object.
- * ``flash_geometry`` (list): List of (Shapely) geometric object resulting
- from ``flashes``. These are generated from ``flashes`` in ``do_flashes()``.
- * ``buffered_paths`` (list): List of (Shapely) polygons resulting from
- *buffering* (or thickening) the ``paths`` with the aperture. These are
- generated from ``paths`` in ``buffer_paths()``.
- **USAGE**::
- g = Gerber()
- g.parse_file(filename)
- g.create_geometry()
- do_something(s.solid_geometry)
- """
- defaults = {
- "steps_per_circle": 40,
- "use_buffer_for_union": True
- }
- def __init__(self, steps_per_circle=None):
- """
- The constructor takes no parameters. Use ``gerber.parse_files()``
- or ``gerber.parse_lines()`` to populate the object from Gerber source.
- :return: Gerber object
- :rtype: Gerber
- """
- # Initialize parent
- Geometry.__init__(self)
- self.solid_geometry = Polygon()
- # Number format
- self.int_digits = 3
- """Number of integer digits in Gerber numbers. Used during parsing."""
- self.frac_digits = 4
- """Number of fraction digits in Gerber numbers. Used during parsing."""
-
- ## Gerber elements ##
- # Apertures {'id':{'type':chr,
- # ['size':float], ['width':float],
- # ['height':float]}, ...}
- self.apertures = {}
- # Aperture Macros
- self.aperture_macros = {}
- # Attributes to be included in serialization
- # Always append to it because it carries contents
- # from Geometry.
- self.ser_attrs += ['int_digits', 'frac_digits', 'apertures',
- 'aperture_macros', 'solid_geometry']
- #### Parser patterns ####
- # FS - Format Specification
- # The format of X and Y must be the same!
- # L-omit leading zeros, T-omit trailing zeros
- # A-absolute notation, I-incremental notation
- self.fmt_re = re.compile(r'%FS([LT])([AI])X(\d)(\d)Y\d\d\*%$')
- # Mode (IN/MM)
- self.mode_re = re.compile(r'^%MO(IN|MM)\*%$')
- # Comment G04|G4
- self.comm_re = re.compile(r'^G0?4(.*)$')
- # AD - Aperture definition
- # Aperture Macro names: Name = [a-zA-Z_.$]{[a-zA-Z_.0-9]+}
- # NOTE: Adding "-" to support output from Upverter.
- self.ad_re = re.compile(r'^%ADD(\d\d+)([a-zA-Z_$\.][a-zA-Z0-9_$\.\-]*)(?:,(.*))?\*%$')
- # AM - Aperture Macro
- # Beginning of macro (Ends with *%):
- #self.am_re = re.compile(r'^%AM([a-zA-Z0-9]*)\*')
- # Tool change
- # May begin with G54 but that is deprecated
- self.tool_re = re.compile(r'^(?:G54)?D(\d\d+)\*$')
- # G01... - Linear interpolation plus flashes with coordinates
- # Operation code (D0x) missing is deprecated... oh well I will support it.
- self.lin_re = re.compile(r'^(?:G0?(1))?(?=.*X([\+-]?\d+))?(?=.*Y([\+-]?\d+))?[XY][^DIJ]*(?:D0?([123]))?\*$')
- # Operation code alone, usually just D03 (Flash)
- self.opcode_re = re.compile(r'^D0?([123])\*$')
- # G02/3... - Circular interpolation with coordinates
- # 2-clockwise, 3-counterclockwise
- # Operation code (D0x) missing is deprecated... oh well I will support it.
- # Optional start with G02 or G03, optional end with D01 or D02 with
- # optional coordinates but at least one in any order.
- self.circ_re = re.compile(r'^(?:G0?([23]))?(?=.*X([\+-]?\d+))?(?=.*Y([\+-]?\d+))' +
- '?(?=.*I([\+-]?\d+))?(?=.*J([\+-]?\d+))?[XYIJ][^D]*(?:D0([12]))?\*$')
- # G01/2/3 Occurring without coordinates
- self.interp_re = re.compile(r'^(?:G0?([123]))\*')
- # Single D74 or multi D75 quadrant for circular interpolation
- self.quad_re = re.compile(r'^G7([45])\*$')
- # Region mode on
- # In region mode, D01 starts a region
- # and D02 ends it. A new region can be started again
- # with D01. All contours must be closed before
- # D02 or G37.
- self.regionon_re = re.compile(r'^G36\*$')
- # Region mode off
- # Will end a region and come off region mode.
- # All contours must be closed before D02 or G37.
- self.regionoff_re = re.compile(r'^G37\*$')
- # End of file
- self.eof_re = re.compile(r'^M02\*')
- # IP - Image polarity
- self.pol_re = re.compile(r'^%IP(POS|NEG)\*%$')
- # LP - Level polarity
- self.lpol_re = re.compile(r'^%LP([DC])\*%$')
- # Units (OBSOLETE)
- self.units_re = re.compile(r'^G7([01])\*$')
- # Absolute/Relative G90/1 (OBSOLETE)
- self.absrel_re = re.compile(r'^G9([01])\*$')
- # Aperture macros
- self.am1_re = re.compile(r'^%AM([^\*]+)\*([^%]+)?(%)?$')
- self.am2_re = re.compile(r'(.*)%$')
- # How to discretize a circle.
- self.steps_per_circ = steps_per_circle or Gerber.defaults['steps_per_circle']
- self.use_buffer_for_union = self.defaults["use_buffer_for_union"]
- def scale(self, factor):
- """
- Scales the objects' geometry on the XY plane by a given factor.
- These are:
- * ``buffered_paths``
- * ``flash_geometry``
- * ``solid_geometry``
- * ``regions``
- NOTE:
- Does not modify the data used to create these elements. If these
- are recreated, the scaling will be lost. This behavior was modified
- because of the complexity reached in this class.
- :param factor: Number by which to scale.
- :type factor: float
- :rtype : None
- """
- ## solid_geometry ???
- # It's a cascaded union of objects.
- self.solid_geometry = affinity.scale(self.solid_geometry, factor,
- factor, origin=(0, 0))
- # # Now buffered_paths, flash_geometry and solid_geometry
- # self.create_geometry()
- def offset(self, vect):
- """
- Offsets the objects' geometry on the XY plane by a given vector.
- These are:
- * ``buffered_paths``
- * ``flash_geometry``
- * ``solid_geometry``
- * ``regions``
- NOTE:
- Does not modify the data used to create these elements. If these
- are recreated, the scaling will be lost. This behavior was modified
- because of the complexity reached in this class.
- :param vect: (x, y) offset vector.
- :type vect: tuple
- :return: None
- """
- dx, dy = vect
- ## Solid geometry
- self.solid_geometry = affinity.translate(self.solid_geometry, xoff=dx, yoff=dy)
- def mirror(self, axis, point):
- """
- Mirrors the object around a specified axis passign through
- the given point. What is affected:
- * ``buffered_paths``
- * ``flash_geometry``
- * ``solid_geometry``
- * ``regions``
- NOTE:
- Does not modify the data used to create these elements. If these
- are recreated, the scaling will be lost. This behavior was modified
- because of the complexity reached in this class.
- :param axis: "X" or "Y" indicates around which axis to mirror.
- :type axis: str
- :param point: [x, y] point belonging to the mirror axis.
- :type point: list
- :return: None
- """
- px, py = point
- xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
- ## solid_geometry ???
- # It's a cascaded union of objects.
- self.solid_geometry = affinity.scale(self.solid_geometry,
- xscale, yscale, origin=(px, py))
- def aperture_parse(self, apertureId, apertureType, apParameters):
- """
- Parse gerber aperture definition into dictionary of apertures.
- The following kinds and their attributes are supported:
- * *Circular (C)*: size (float)
- * *Rectangle (R)*: width (float), height (float)
- * *Obround (O)*: width (float), height (float).
- * *Polygon (P)*: diameter(float), vertices(int), [rotation(float)]
- * *Aperture Macro (AM)*: macro (ApertureMacro), modifiers (list)
- :param apertureId: Id of the aperture being defined.
- :param apertureType: Type of the aperture.
- :param apParameters: Parameters of the aperture.
- :type apertureId: str
- :type apertureType: str
- :type apParameters: str
- :return: Identifier of the aperture.
- :rtype: str
- """
- # Found some Gerber with a leading zero in the aperture id and the
- # referenced it without the zero, so this is a hack to handle that.
- apid = str(int(apertureId))
- try: # Could be empty for aperture macros
- paramList = apParameters.split('X')
- except:
- paramList = None
- if apertureType == "C": # Circle, example: %ADD11C,0.1*%
- self.apertures[apid] = {"type": "C",
- "size": float(paramList[0])}
- return apid
-
- if apertureType == "R": # Rectangle, example: %ADD15R,0.05X0.12*%
- self.apertures[apid] = {"type": "R",
- "width": float(paramList[0]),
- "height": float(paramList[1]),
- "size": sqrt(float(paramList[0])**2 + float(paramList[1])**2)} # Hack
- return apid
- if apertureType == "O": # Obround
- self.apertures[apid] = {"type": "O",
- "width": float(paramList[0]),
- "height": float(paramList[1]),
- "size": sqrt(float(paramList[0])**2 + float(paramList[1])**2)} # Hack
- return apid
-
- if apertureType == "P": # Polygon (regular)
- self.apertures[apid] = {"type": "P",
- "diam": float(paramList[0]),
- "nVertices": int(paramList[1]),
- "size": float(paramList[0])} # Hack
- if len(paramList) >= 3:
- self.apertures[apid]["rotation"] = float(paramList[2])
- return apid
- if apertureType in self.aperture_macros:
- self.apertures[apid] = {"type": "AM",
- "macro": self.aperture_macros[apertureType],
- "modifiers": paramList}
- return apid
- log.warning("Aperture not implemented: %s" % str(apertureType))
- return None
-
- def parse_file(self, filename, follow=False):
- """
- Calls Gerber.parse_lines() with generator of lines
- read from the given file. Will split the lines if multiple
- statements are found in a single original line.
- The following line is split into two::
- G54D11*G36*
- First is ``G54D11*`` and seconds is ``G36*``.
- :param filename: Gerber file to parse.
- :type filename: str
- :param follow: If true, will not create polygons, just lines
- following the gerber path.
- :type follow: bool
- :return: None
- """
- with open(filename, 'r') as gfile:
- def line_generator():
- for line in gfile:
- line = line.strip(' \r\n')
- while len(line) > 0:
- # If ends with '%' leave as is.
- if line[-1] == '%':
- yield line
- break
- # Split after '*' if any.
- starpos = line.find('*')
- if starpos > -1:
- cleanline = line[:starpos + 1]
- yield cleanline
- line = line[starpos + 1:]
- # Otherwise leave as is.
- else:
- # yield cleanline
- yield line
- break
- self.parse_lines(line_generator(), follow=follow)
- #@profile
- def parse_lines(self, glines, follow=False):
- """
- Main Gerber parser. Reads Gerber and populates ``self.paths``, ``self.apertures``,
- ``self.flashes``, ``self.regions`` and ``self.units``.
- :param glines: Gerber code as list of strings, each element being
- one line of the source file.
- :type glines: list
- :param follow: If true, will not create polygons, just lines
- following the gerber path.
- :type follow: bool
- :return: None
- :rtype: None
- """
- # Coordinates of the current path, each is [x, y]
- path = []
- # Polygons are stored here until there is a change in polarity.
- # Only then they are combined via cascaded_union and added or
- # subtracted from solid_geometry. This is ~100 times faster than
- # applyng a union for every new polygon.
- poly_buffer = []
- last_path_aperture = None
- current_aperture = None
- # 1,2 or 3 from "G01", "G02" or "G03"
- current_interpolation_mode = None
- # 1 or 2 from "D01" or "D02"
- # Note this is to support deprecated Gerber not putting
- # an operation code at the end of every coordinate line.
- current_operation_code = None
- # Current coordinates
- current_x = None
- current_y = None
- # Absolute or Relative/Incremental coordinates
- # Not implemented
- absolute = True
- # How to interpret circular interpolation: SINGLE or MULTI
- quadrant_mode = None
- # Indicates we are parsing an aperture macro
- current_macro = None
- # Indicates the current polarity: D-Dark, C-Clear
- current_polarity = 'D'
- # If a region is being defined
- making_region = False
- #### Parsing starts here ####
- line_num = 0
- gline = ""
- try:
- for gline in glines:
- line_num += 1
- ### Cleanup
- gline = gline.strip(' \r\n')
- #log.debug("%3s %s" % (line_num, gline))
- ### Aperture Macros
- # Having this at the beggining will slow things down
- # but macros can have complicated statements than could
- # be caught by other patterns.
- if current_macro is None: # No macro started yet
- match = self.am1_re.search(gline)
- # Start macro if match, else not an AM, carry on.
- if match:
- log.debug("Starting macro. Line %d: %s" % (line_num, gline))
- current_macro = match.group(1)
- self.aperture_macros[current_macro] = ApertureMacro(name=current_macro)
- if match.group(2): # Append
- self.aperture_macros[current_macro].append(match.group(2))
- if match.group(3): # Finish macro
- #self.aperture_macros[current_macro].parse_content()
- current_macro = None
- log.debug("Macro complete in 1 line.")
- continue
- else: # Continue macro
- log.debug("Continuing macro. Line %d." % line_num)
- match = self.am2_re.search(gline)
- if match: # Finish macro
- log.debug("End of macro. Line %d." % line_num)
- self.aperture_macros[current_macro].append(match.group(1))
- #self.aperture_macros[current_macro].parse_content()
- current_macro = None
- else: # Append
- self.aperture_macros[current_macro].append(gline)
- continue
- ### G01 - Linear interpolation plus flashes
- # Operation code (D0x) missing is deprecated... oh well I will support it.
- # REGEX: r'^(?:G0?(1))?(?:X(-?\d+))?(?:Y(-?\d+))?(?:D0([123]))?\*$'
- match = self.lin_re.search(gline)
- if match:
- # Dxx alone?
- # if match.group(1) is None and match.group(2) is None and match.group(3) is None:
- # try:
- # current_operation_code = int(match.group(4))
- # except:
- # pass # A line with just * will match too.
- # continue
- # NOTE: Letting it continue allows it to react to the
- # operation code.
- # Parse coordinates
- if match.group(2) is not None:
- current_x = parse_gerber_number(match.group(2), self.frac_digits)
- if match.group(3) is not None:
- current_y = parse_gerber_number(match.group(3), self.frac_digits)
- # Parse operation code
- if match.group(4) is not None:
- current_operation_code = int(match.group(4))
- # Pen down: add segment
- if current_operation_code == 1:
- path.append([current_x, current_y])
- last_path_aperture = current_aperture
- elif current_operation_code == 2:
- if len(path) > 1:
- ## --- BUFFERED ---
- if making_region:
- geo = Polygon(path)
- else:
- if last_path_aperture is None:
- log.warning("No aperture defined for curent path. (%d)" % line_num)
- width = self.apertures[last_path_aperture]["size"] # TODO: WARNING this should fail!
- #log.debug("Line %d: Setting aperture to %s before buffering." % (line_num, last_path_aperture))
- if follow:
- geo = LineString(path)
- else:
- geo = LineString(path).buffer(width / 2)
- if not geo.is_empty: poly_buffer.append(geo)
- path = [[current_x, current_y]] # Start new path
- # Flash
- # Not allowed in region mode.
- elif current_operation_code == 3:
- # Create path draw so far.
- if len(path) > 1:
- # --- Buffered ----
- width = self.apertures[last_path_aperture]["size"]
- geo = LineString(path).buffer(width / 2)
- if not geo.is_empty: poly_buffer.append(geo)
- # Reset path starting point
- path = [[current_x, current_y]]
- # --- BUFFERED ---
- # Draw the flash
- flash = Gerber.create_flash_geometry(Point([current_x, current_y]),
- self.apertures[current_aperture])
- if not flash.is_empty: poly_buffer.append(flash)
- continue
- ### G02/3 - Circular interpolation
- # 2-clockwise, 3-counterclockwise
- match = self.circ_re.search(gline)
- if match:
- arcdir = [None, None, "cw", "ccw"]
- mode, x, y, i, j, d = match.groups()
- try:
- x = parse_gerber_number(x, self.frac_digits)
- except:
- x = current_x
- try:
- y = parse_gerber_number(y, self.frac_digits)
- except:
- y = current_y
- try:
- i = parse_gerber_number(i, self.frac_digits)
- except:
- i = 0
- try:
- j = parse_gerber_number(j, self.frac_digits)
- except:
- j = 0
- if quadrant_mode is None:
- log.error("Found arc without preceding quadrant specification G74 or G75. (%d)" % line_num)
- log.error(gline)
- continue
- if mode is None and current_interpolation_mode not in [2, 3]:
- log.error("Found arc without circular interpolation mode defined. (%d)" % line_num)
- log.error(gline)
- continue
- elif mode is not None:
- current_interpolation_mode = int(mode)
- # Set operation code if provided
- if d is not None:
- current_operation_code = int(d)
- # Nothing created! Pen Up.
- if current_operation_code == 2:
- log.warning("Arc with D2. (%d)" % line_num)
- if len(path) > 1:
- if last_path_aperture is None:
- log.warning("No aperture defined for curent path. (%d)" % line_num)
- # --- BUFFERED ---
- width = self.apertures[last_path_aperture]["size"]
- buffered = LineString(path).buffer(width / 2)
- if not buffered.is_empty: poly_buffer.append(buffered)
- current_x = x
- current_y = y
- path = [[current_x, current_y]] # Start new path
- continue
- # Flash should not happen here
- if current_operation_code == 3:
- log.error("Trying to flash within arc. (%d)" % line_num)
- continue
- if quadrant_mode == 'MULTI':
- center = [i + current_x, j + current_y]
- radius = sqrt(i ** 2 + j ** 2)
- start = arctan2(-j, -i) # Start angle
- # Numerical errors might prevent start == stop therefore
- # we check ahead of time. This should result in a
- # 360 degree arc.
- if current_x == x and current_y == y:
- stop = start
- else:
- stop = arctan2(-center[1] + y, -center[0] + x) # Stop angle
- this_arc = arc(center, radius, start, stop,
- arcdir[current_interpolation_mode],
- self.steps_per_circ)
- # The last point in the computed arc can have
- # numerical errors. The exact final point is the
- # specified (x, y). Replace.
- this_arc[-1] = (x, y)
- # Last point in path is current point
- # current_x = this_arc[-1][0]
- # current_y = this_arc[-1][1]
- current_x, current_y = x, y
- # Append
- path += this_arc
- last_path_aperture = current_aperture
- continue
- if quadrant_mode == 'SINGLE':
- center_candidates = [
- [i + current_x, j + current_y],
- [-i + current_x, j + current_y],
- [i + current_x, -j + current_y],
- [-i + current_x, -j + current_y]
- ]
- valid = False
- log.debug("I: %f J: %f" % (i, j))
- for center in center_candidates:
- radius = sqrt(i ** 2 + j ** 2)
- # Make sure radius to start is the same as radius to end.
- radius2 = sqrt((center[0] - x) ** 2 + (center[1] - y) ** 2)
- if radius2 < radius * 0.95 or radius2 > radius * 1.05:
- continue # Not a valid center.
- # Correct i and j and continue as with multi-quadrant.
- i = center[0] - current_x
- j = center[1] - current_y
- start = arctan2(-j, -i) # Start angle
- stop = arctan2(-center[1] + y, -center[0] + x) # Stop angle
- angle = abs(arc_angle(start, stop, arcdir[current_interpolation_mode]))
- log.debug("ARC START: %f, %f CENTER: %f, %f STOP: %f, %f" %
- (current_x, current_y, center[0], center[1], x, y))
- log.debug("START Ang: %f, STOP Ang: %f, DIR: %s, ABS: %.12f <= %.12f: %s" %
- (start * 180 / pi, stop * 180 / pi, arcdir[current_interpolation_mode],
- angle * 180 / pi, pi / 2 * 180 / pi, angle <= (pi + 1e-6) / 2))
- if angle <= (pi + 1e-6) / 2:
- log.debug("########## ACCEPTING ARC ############")
- this_arc = arc(center, radius, start, stop,
- arcdir[current_interpolation_mode],
- self.steps_per_circ)
- # Replace with exact values
- this_arc[-1] = (x, y)
- # current_x = this_arc[-1][0]
- # current_y = this_arc[-1][1]
- current_x, current_y = x, y
- path += this_arc
- last_path_aperture = current_aperture
- valid = True
- break
- if valid:
- continue
- else:
- log.warning("Invalid arc in line %d." % line_num)
- ### Operation code alone
- # Operation code alone, usually just D03 (Flash)
- # self.opcode_re = re.compile(r'^D0?([123])\*$')
- match = self.opcode_re.search(gline)
- if match:
- current_operation_code = int(match.group(1))
- if current_operation_code == 3:
- ## --- Buffered ---
- try:
- log.debug("Bare op-code %d." % current_operation_code)
- # flash = Gerber.create_flash_geometry(Point(path[-1]),
- # self.apertures[current_aperture])
- flash = Gerber.create_flash_geometry(Point(current_x, current_y),
- self.apertures[current_aperture])
- if not flash.is_empty: poly_buffer.append(flash)
- except IndexError:
- log.warning("Line %d: %s -> Nothing there to flash!" % (line_num, gline))
- continue
- ### G74/75* - Single or multiple quadrant arcs
- match = self.quad_re.search(gline)
- if match:
- if match.group(1) == '4':
- quadrant_mode = 'SINGLE'
- else:
- quadrant_mode = 'MULTI'
- continue
- ### G36* - Begin region
- if self.regionon_re.search(gline):
- if len(path) > 1:
- # Take care of what is left in the path
- ## --- Buffered ---
- width = self.apertures[last_path_aperture]["size"]
- geo = LineString(path).buffer(width/2)
- if not geo.is_empty: poly_buffer.append(geo)
- path = [path[-1]]
- making_region = True
- continue
- ### G37* - End region
- if self.regionoff_re.search(gline):
- making_region = False
- # Only one path defines region?
- # This can happen if D02 happened before G37 and
- # is not and error.
- if len(path) < 3:
- # print "ERROR: Path contains less than 3 points:"
- # print path
- # print "Line (%d): " % line_num, gline
- # path = []
- #path = [[current_x, current_y]]
- continue
- # For regions we may ignore an aperture that is None
- # self.regions.append({"polygon": Polygon(path),
- # "aperture": last_path_aperture})
- # --- Buffered ---
- region = Polygon(path)
- if not region.is_valid:
- region = region.buffer(0)
- if not region.is_empty: poly_buffer.append(region)
- path = [[current_x, current_y]] # Start new path
- continue
- ### Aperture definitions %ADD...
- match = self.ad_re.search(gline)
- if match:
- log.info("Found aperture definition. Line %d: %s" % (line_num, gline))
- self.aperture_parse(match.group(1), match.group(2), match.group(3))
- continue
- ### G01/2/3* - Interpolation mode change
- # Can occur along with coordinates and operation code but
- # sometimes by itself (handled here).
- # Example: G01*
- match = self.interp_re.search(gline)
- if match:
- current_interpolation_mode = int(match.group(1))
- continue
- ### Tool/aperture change
- # Example: D12*
- match = self.tool_re.search(gline)
- if match:
- current_aperture = match.group(1)
- log.debug("Line %d: Aperture change to (%s)" % (line_num, match.group(1)))
- log.debug(self.apertures[current_aperture])
- # Take care of the current path with the previous tool
- if len(path) > 1:
- # --- Buffered ----
- width = self.apertures[last_path_aperture]["size"]
- geo = LineString(path).buffer(width / 2)
- if not geo.is_empty: poly_buffer.append(geo)
- path = [path[-1]]
- continue
- ### Polarity change
- # Example: %LPD*% or %LPC*%
- # If polarity changes, creates geometry from current
- # buffer, then adds or subtracts accordingly.
- match = self.lpol_re.search(gline)
- if match:
- if len(path) > 1 and current_polarity != match.group(1):
- # --- Buffered ----
- width = self.apertures[last_path_aperture]["size"]
- geo = LineString(path).buffer(width / 2)
- if not geo.is_empty: poly_buffer.append(geo)
- path = [path[-1]]
- # --- Apply buffer ---
- # If added for testing of bug #83
- # TODO: Remove when bug fixed
- if len(poly_buffer) > 0:
- if current_polarity == 'D':
- self.solid_geometry = self.solid_geometry.union(cascaded_union(poly_buffer))
- else:
- self.solid_geometry = self.solid_geometry.difference(cascaded_union(poly_buffer))
- poly_buffer = []
- current_polarity = match.group(1)
- continue
- ### Number format
- # Example: %FSLAX24Y24*%
- # TODO: This is ignoring most of the format. Implement the rest.
- match = self.fmt_re.search(gline)
- if match:
- absolute = {'A': True, 'I': False}
- self.int_digits = int(match.group(3))
- self.frac_digits = int(match.group(4))
- continue
- ### Mode (IN/MM)
- # Example: %MOIN*%
- match = self.mode_re.search(gline)
- if match:
- #self.units = match.group(1)
- # Changed for issue #80
- self.convert_units(match.group(1))
- continue
- ### Units (G70/1) OBSOLETE
- match = self.units_re.search(gline)
- if match:
- #self.units = {'0': 'IN', '1': 'MM'}[match.group(1)]
- # Changed for issue #80
- self.convert_units({'0': 'IN', '1': 'MM'}[match.group(1)])
- continue
- ### Absolute/relative coordinates G90/1 OBSOLETE
- match = self.absrel_re.search(gline)
- if match:
- absolute = {'0': True, '1': False}[match.group(1)]
- continue
- #### Ignored lines
- ## Comments
- match = self.comm_re.search(gline)
- if match:
- continue
- ## EOF
- match = self.eof_re.search(gline)
- if match:
- continue
- ### Line did not match any pattern. Warn user.
- log.warning("Line ignored (%d): %s" % (line_num, gline))
- if len(path) > 1:
- # EOF, create shapely LineString if something still in path
- ## --- Buffered ---
- width = self.apertures[last_path_aperture]["size"]
- geo = LineString(path).buffer(width / 2)
- if not geo.is_empty: poly_buffer.append(geo)
- # --- Apply buffer ---
- log.warn("Joining %d polygons." % len(poly_buffer))
- if self.use_buffer_for_union:
- log.debug("Union by buffer...")
- new_poly = MultiPolygon(poly_buffer)
- new_poly = new_poly.buffer(0.00000001)
- new_poly = new_poly.buffer(-0.00000001)
- log.warn("Union(buffer) done.")
- else:
- log.debug("Union by union()...")
- new_poly = cascaded_union(poly_buffer)
- new_poly = new_poly.buffer(0)
- log.warn("Union done.")
- if current_polarity == 'D':
- self.solid_geometry = self.solid_geometry.union(new_poly)
- else:
- self.solid_geometry = self.solid_geometry.difference(new_poly)
- except Exception, err:
- ex_type, ex, tb = sys.exc_info()
- traceback.print_tb(tb)
- #print traceback.format_exc()
- log.error("PARSING FAILED. Line %d: %s" % (line_num, gline))
- raise ParseError("Line %d: %s" % (line_num, gline), repr(err))
- @staticmethod
- def create_flash_geometry(location, aperture):
- log.debug('Flashing @%s, Aperture: %s' % (location, aperture))
- if type(location) == list:
- location = Point(location)
- if aperture['type'] == 'C': # Circles
- return location.buffer(aperture['size'] / 2)
- if aperture['type'] == 'R': # Rectangles
- loc = location.coords[0]
- width = aperture['width']
- height = aperture['height']
- minx = loc[0] - width / 2
- maxx = loc[0] + width / 2
- miny = loc[1] - height / 2
- maxy = loc[1] + height / 2
- return shply_box(minx, miny, maxx, maxy)
- if aperture['type'] == 'O': # Obround
- loc = location.coords[0]
- width = aperture['width']
- height = aperture['height']
- if width > height:
- p1 = Point(loc[0] + 0.5 * (width - height), loc[1])
- p2 = Point(loc[0] - 0.5 * (width - height), loc[1])
- c1 = p1.buffer(height * 0.5)
- c2 = p2.buffer(height * 0.5)
- else:
- p1 = Point(loc[0], loc[1] + 0.5 * (height - width))
- p2 = Point(loc[0], loc[1] - 0.5 * (height - width))
- c1 = p1.buffer(width * 0.5)
- c2 = p2.buffer(width * 0.5)
- return cascaded_union([c1, c2]).convex_hull
- if aperture['type'] == 'P': # Regular polygon
- loc = location.coords[0]
- diam = aperture['diam']
- n_vertices = aperture['nVertices']
- points = []
- for i in range(0, n_vertices):
- x = loc[0] + 0.5 * diam * (cos(2 * pi * i / n_vertices))
- y = loc[1] + 0.5 * diam * (sin(2 * pi * i / n_vertices))
- points.append((x, y))
- ply = Polygon(points)
- if 'rotation' in aperture:
- ply = affinity.rotate(ply, aperture['rotation'])
- return ply
- if aperture['type'] == 'AM': # Aperture Macro
- loc = location.coords[0]
- flash_geo = aperture['macro'].make_geometry(aperture['modifiers'])
- if flash_geo.is_empty:
- log.warning("Empty geometry for Aperture Macro: %s" % str(aperture['macro'].name))
- return affinity.translate(flash_geo, xoff=loc[0], yoff=loc[1])
- log.warning("Unknown aperture type: %s" % aperture['type'])
- return None
-
- def create_geometry(self):
- """
- Geometry from a Gerber file is made up entirely of polygons.
- Every stroke (linear or circular) has an aperture which gives
- it thickness. Additionally, aperture strokes have non-zero area,
- and regions naturally do as well.
- :rtype : None
- :return: None
- """
- # self.buffer_paths()
- #
- # self.fix_regions()
- #
- # self.do_flashes()
- #
- # self.solid_geometry = cascaded_union(self.buffered_paths +
- # [poly['polygon'] for poly in self.regions] +
- # self.flash_geometry)
- def get_bounding_box(self, margin=0.0, rounded=False):
- """
- Creates and returns a rectangular polygon bounding at a distance of
- margin from the object's ``solid_geometry``. If margin > 0, the polygon
- can optionally have rounded corners of radius equal to margin.
- :param margin: Distance to enlarge the rectangular bounding
- box in both positive and negative, x and y axes.
- :type margin: float
- :param rounded: Wether or not to have rounded corners.
- :type rounded: bool
- :return: The bounding box.
- :rtype: Shapely.Polygon
- """
- bbox = self.solid_geometry.envelope.buffer(margin)
- if not rounded:
- bbox = bbox.envelope
- return bbox
- class Excellon(Geometry):
- """
- *ATTRIBUTES*
- * ``tools`` (dict): The key is the tool name and the value is
- a dictionary specifying the tool:
- ================ ====================================
- Key Value
- ================ ====================================
- C Diameter of the tool
- Others Not supported (Ignored).
- ================ ====================================
- * ``drills`` (list): Each is a dictionary:
- ================ ====================================
- Key Value
- ================ ====================================
- point (Shapely.Point) Where to drill
- tool (str) A key in ``tools``
- ================ ====================================
- """
- defaults = {
- "zeros": "L"
- }
- def __init__(self, zeros=None):
- """
- The constructor takes no parameters.
- :return: Excellon object.
- :rtype: Excellon
- """
- Geometry.__init__(self)
-
- self.tools = {}
-
- self.drills = []
- ## IN|MM -> Units are inherited from Geometry
- #self.units = units
- # Trailing "T" or leading "L" (default)
- #self.zeros = "T"
- self.zeros = zeros or self.defaults["zeros"]
- # Attributes to be included in serialization
- # Always append to it because it carries contents
- # from Geometry.
- self.ser_attrs += ['tools', 'drills', 'zeros']
- #### Patterns ####
- # Regex basics:
- # ^ - beginning
- # $ - end
- # *: 0 or more, +: 1 or more, ?: 0 or 1
- # M48 - Beggining of Part Program Header
- self.hbegin_re = re.compile(r'^M48$')
- # M95 or % - End of Part Program Header
- # NOTE: % has different meaning in the body
- self.hend_re = re.compile(r'^(?:M95|%)$')
- # FMAT Excellon format
- # Ignored in the parser
- #self.fmat_re = re.compile(r'^FMAT,([12])$')
- # Number format and units
- # INCH uses 6 digits
- # METRIC uses 5/6
- self.units_re = re.compile(r'^(INCH|METRIC)(?:,([TL])Z)?$')
- # Tool definition/parameters (?= is look-ahead
- # NOTE: This might be an overkill!
- # self.toolset_re = re.compile(r'^T(0?\d|\d\d)(?=.*C(\d*\.?\d*))?' +
- # r'(?=.*F(\d*\.?\d*))?(?=.*S(\d*\.?\d*))?' +
- # r'(?=.*B(\d*\.?\d*))?(?=.*H(\d*\.?\d*))?' +
- # r'(?=.*Z([-\+]?\d*\.?\d*))?[CFSBHT]')
- self.toolset_re = re.compile(r'^T(\d+)(?=.*C(\d*\.?\d*))?' +
- r'(?=.*F(\d*\.?\d*))?(?=.*S(\d*\.?\d*))?' +
- r'(?=.*B(\d*\.?\d*))?(?=.*H(\d*\.?\d*))?' +
- r'(?=.*Z([-\+]?\d*\.?\d*))?[CFSBHT]')
- # Tool select
- # Can have additional data after tool number but
- # is ignored if present in the header.
- # Warning: This will match toolset_re too.
- # self.toolsel_re = re.compile(r'^T((?:\d\d)|(?:\d))')
- self.toolsel_re = re.compile(r'^T(\d+)')
- # Comment
- self.comm_re = re.compile(r'^;(.*)$')
- # Absolute/Incremental G90/G91
- self.absinc_re = re.compile(r'^G9([01])$')
- # Modes of operation
- # 1-linear, 2-circCW, 3-cirCCW, 4-vardwell, 5-Drill
- self.modes_re = re.compile(r'^G0([012345])')
- # Measuring mode
- # 1-metric, 2-inch
- self.meas_re = re.compile(r'^M7([12])$')
- # Coordinates
- #self.xcoord_re = re.compile(r'^X(\d*\.?\d*)(?:Y\d*\.?\d*)?$')
- #self.ycoord_re = re.compile(r'^(?:X\d*\.?\d*)?Y(\d*\.?\d*)$')
- self.coordsperiod_re = re.compile(r'(?=.*X([-\+]?\d*\.\d*))?(?=.*Y([-\+]?\d*\.\d*))?[XY]')
- self.coordsnoperiod_re = re.compile(r'(?!.*\.)(?=.*X([-\+]?\d*))?(?=.*Y([-\+]?\d*))?[XY]')
- # R - Repeat hole (# times, X offset, Y offset)
- self.rep_re = re.compile(r'^R(\d+)(?=.*[XY])+(?:X([-\+]?\d*\.?\d*))?(?:Y([-\+]?\d*\.?\d*))?$')
- # Various stop/pause commands
- self.stop_re = re.compile(r'^((G04)|(M09)|(M06)|(M00)|(M30))')
- # Parse coordinates
- self.leadingzeros_re = re.compile(r'^[-\+]?(0*)(\d*)')
-
- def parse_file(self, filename):
- """
- Reads the specified file as array of lines as
- passes it to ``parse_lines()``.
- :param filename: The file to be read and parsed.
- :type filename: str
- :return: None
- """
- efile = open(filename, 'r')
- estr = efile.readlines()
- efile.close()
- self.parse_lines(estr)
- def parse_lines(self, elines):
- """
- Main Excellon parser.
- :param elines: List of strings, each being a line of Excellon code.
- :type elines: list
- :return: None
- """
- # State variables
- current_tool = ""
- in_header = False
- current_x = None
- current_y = None
- #### Parsing starts here ####
- line_num = 0 # Line number
- eline = ""
- try:
- for eline in elines:
- line_num += 1
- #log.debug("%3d %s" % (line_num, str(eline)))
- ### Cleanup lines
- eline = eline.strip(' \r\n')
- ## Header Begin (M48) ##
- if self.hbegin_re.search(eline):
- in_header = True
- continue
- ## Header End ##
- if self.hend_re.search(eline):
- in_header = False
- continue
- ## Alternative units format M71/M72
- # Supposed to be just in the body (yes, the body)
- # but some put it in the header (PADS for example).
- # Will detect anywhere. Occurrence will change the
- # object's units.
- match = self.meas_re.match(eline)
- if match:
- #self.units = {"1": "MM", "2": "IN"}[match.group(1)]
- # Modified for issue #80
- self.convert_units({"1": "MM", "2": "IN"}[match.group(1)])
- log.debug(" Units: %s" % self.units)
- continue
- #### Body ####
- if not in_header:
- ## Tool change ##
- match = self.toolsel_re.search(eline)
- if match:
- current_tool = str(int(match.group(1)))
- log.debug("Tool change: %s" % current_tool)
- continue
- ## Coordinates without period ##
- match = self.coordsnoperiod_re.search(eline)
- if match:
- try:
- #x = float(match.group(1))/10000
- x = self.parse_number(match.group(1))
- current_x = x
- except TypeError:
- x = current_x
- try:
- #y = float(match.group(2))/10000
- y = self.parse_number(match.group(2))
- current_y = y
- except TypeError:
- y = current_y
- if x is None or y is None:
- log.error("Missing coordinates")
- continue
- self.drills.append({'point': Point((x, y)), 'tool': current_tool})
- log.debug("{:15} {:8} {:8}".format(eline, x, y))
- continue
- ## Coordinates with period: Use literally. ##
- match = self.coordsperiod_re.search(eline)
- if match:
- try:
- x = float(match.group(1))
- current_x = x
- except TypeError:
- x = current_x
- try:
- y = float(match.group(2))
- current_y = y
- except TypeError:
- y = current_y
- if x is None or y is None:
- log.error("Missing coordinates")
- continue
- self.drills.append({'point': Point((x, y)), 'tool': current_tool})
- log.debug("{:15} {:8} {:8}".format(eline, x, y))
- continue
- #### Header ####
- if in_header:
- ## Tool definitions ##
- match = self.toolset_re.search(eline)
- if match:
- name = str(int(match.group(1)))
- spec = {
- "C": float(match.group(2)),
- # "F": float(match.group(3)),
- # "S": float(match.group(4)),
- # "B": float(match.group(5)),
- # "H": float(match.group(6)),
- # "Z": float(match.group(7))
- }
- self.tools[name] = spec
- log.debug(" Tool definition: %s %s" % (name, spec))
- continue
- ## Units and number format ##
- match = self.units_re.match(eline)
- if match:
- self.zeros = match.group(2) or self.zeros # "T" or "L". Might be empty
- #self.units = {"INCH": "IN", "METRIC": "MM"}[match.group(1)]
- # Modified for issue #80
- self.convert_units({"INCH": "IN", "METRIC": "MM"}[match.group(1)])
- log.debug(" Units/Format: %s %s" % (self.units, self.zeros))
- continue
- log.warning("Line ignored: %s" % eline)
- log.info("Zeros: %s, Units %s." % (self.zeros, self.units))
- except Exception as e:
- log.error("PARSING FAILED. Line %d: %s" % (line_num, eline))
- raise
-
- def parse_number(self, number_str):
- """
- Parses coordinate numbers without period.
- :param number_str: String representing the numerical value.
- :type number_str: str
- :return: Floating point representation of the number
- :rtype: foat
- """
- if self.zeros == "L":
- # With leading zeros, when you type in a coordinate,
- # the leading zeros must always be included. Trailing zeros
- # are unneeded and may be left off. The CNC-7 will automatically add them.
- # r'^[-\+]?(0*)(\d*)'
- # 6 digits are divided by 10^4
- # If less than size digits, they are automatically added,
- # 5 digits then are divided by 10^3 and so on.
- match = self.leadingzeros_re.search(number_str)
- if self.units.lower() == "in":
- return float(number_str) / \
- (10 ** (len(match.group(1)) + len(match.group(2)) - 2))
- else:
- return float(number_str) / \
- (10 ** (len(match.group(1)) + len(match.group(2)) - 3))
- else: # Trailing
- # You must show all zeros to the right of the number and can omit
- # all zeros to the left of the number. The CNC-7 will count the number
- # of digits you typed and automatically fill in the missing zeros.
- if self.units.lower() == "in": # Inches is 00.0000
- return float(number_str) / 10000
- else:
- return float(number_str) / 1000 # Metric is 000.000
- def create_geometry(self):
- """
- Creates circles of the tool diameter at every point
- specified in ``self.drills``.
- :return: None
- """
- self.solid_geometry = []
- for drill in self.drills:
- # poly = drill['point'].buffer(self.tools[drill['tool']]["C"]/2.0)
- tooldia = self.tools[drill['tool']]['C']
- poly = drill['point'].buffer(tooldia / 2.0)
- self.solid_geometry.append(poly)
- def scale(self, factor):
- """
- Scales geometry on the XY plane in the object by a given factor.
- Tool sizes, feedrates an Z-plane dimensions are untouched.
- :param factor: Number by which to scale the object.
- :type factor: float
- :return: None
- :rtype: NOne
- """
- # Drills
- for drill in self.drills:
- drill['point'] = affinity.scale(drill['point'], factor, factor, origin=(0, 0))
- self.create_geometry()
- def offset(self, vect):
- """
- Offsets geometry on the XY plane in the object by a given vector.
- :param vect: (x, y) offset vector.
- :type vect: tuple
- :return: None
- """
- dx, dy = vect
- # Drills
- for drill in self.drills:
- drill['point'] = affinity.translate(drill['point'], xoff=dx, yoff=dy)
- # Recreate geometry
- self.create_geometry()
- def mirror(self, axis, point):
- """
- :param axis: "X" or "Y" indicates around which axis to mirror.
- :type axis: str
- :param point: [x, y] point belonging to the mirror axis.
- :type point: list
- :return: None
- """
- px, py = point
- xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
- # Modify data
- for drill in self.drills:
- drill['point'] = affinity.scale(drill['point'], xscale, yscale, origin=(px, py))
- # Recreate geometry
- self.create_geometry()
- def convert_units(self, units):
- factor = Geometry.convert_units(self, units)
- # Tools
- for tname in self.tools:
- self.tools[tname]["C"] *= factor
- self.create_geometry()
- return factor
- class CNCjob(Geometry):
- """
- Represents work to be done by a CNC machine.
- *ATTRIBUTES*
- * ``gcode_parsed`` (list): Each is a dictionary:
- ===================== =========================================
- Key Value
- ===================== =========================================
- geom (Shapely.LineString) Tool path (XY plane)
- kind (string) "AB", A is "T" (travel) or
- "C" (cut). B is "F" (fast) or "S" (slow).
- ===================== =========================================
- """
- defaults = {
- "zdownrate": None,
- "coordinate_format": "X%.4fY%.4f"
- }
- def __init__(self,
- units="in",
- kind="generic",
- z_move=0.1,
- feedrate=3.0,
- z_cut=-0.002,
- tooldia=0.0,
- zdownrate=None,
- spindlespeed=None):
- Geometry.__init__(self)
- self.kind = kind
- self.units = units
- self.z_cut = z_cut
- self.z_move = z_move
- self.feedrate = feedrate
- self.tooldia = tooldia
- self.unitcode = {"IN": "G20", "MM": "G21"}
- self.pausecode = "G04 P1"
- self.feedminutecode = "G94"
- self.absolutecode = "G90"
- self.gcode = ""
- self.input_geometry_bounds = None
- self.gcode_parsed = None
- self.steps_per_circ = 20 # Used when parsing G-code arcs
- if zdownrate is not None:
- self.zdownrate = float(zdownrate)
- elif CNCjob.defaults["zdownrate"] is not None:
- self.zdownrate = float(CNCjob.defaults["zdownrate"])
- else:
- self.zdownrate = None
- self.spindlespeed = spindlespeed
- # Attributes to be included in serialization
- # Always append to it because it carries contents
- # from Geometry.
- self.ser_attrs += ['kind', 'z_cut', 'z_move', 'feedrate', 'tooldia',
- 'gcode', 'input_geometry_bounds', 'gcode_parsed',
- 'steps_per_circ']
- def convert_units(self, units):
- factor = Geometry.convert_units(self, units)
- log.debug("CNCjob.convert_units()")
- self.z_cut *= factor
- self.z_move *= factor
- self.feedrate *= factor
- self.tooldia *= factor
- return factor
- def generate_from_excellon_by_tool(self, exobj, tools="all",
- toolchange=False, toolchangez=0.1):
- """
- Creates gcode for this object from an Excellon object
- for the specified tools.
- :param exobj: Excellon object to process
- :type exobj: Excellon
- :param tools: Comma separated tool names
- :type: tools: str
- :return: None
- :rtype: None
- """
- log.debug("Creating CNC Job from Excellon...")
- # Tools
- if tools == "all":
- tools = [tool for tool in exobj.tools]
- else:
- tools = [x.strip() for x in tools.split(",")]
- tools = filter(lambda i: i in exobj.tools, tools)
- log.debug("Tools are: %s" % str(tools))
- # Points (Group by tool)
- points = {}
- for drill in exobj.drills:
- if drill['tool'] in tools:
- try:
- points[drill['tool']].append(drill['point'])
- except KeyError:
- points[drill['tool']] = [drill['point']]
- #log.debug("Found %d drills." % len(points))
- self.gcode = []
- # Basic G-Code macros
- t = "G00 " + CNCjob.defaults["coordinate_format"] + "\n"
- down = "G01 Z%.4f\n" % self.z_cut
- up = "G01 Z%.4f\n" % self.z_move
- # Initialization
- gcode = self.unitcode[self.units.upper()] + "\n"
- gcode += self.absolutecode + "\n"
- gcode += self.feedminutecode + "\n"
- gcode += "F%.2f\n" % self.feedrate
- gcode += "G00 Z%.4f\n" % self.z_move # Move to travel height
- if self.spindlespeed is not None:
- gcode += "M03 S%d\n" % int(self.spindlespeed) # Spindle start with configured speed
- else:
- gcode += "M03\n" # Spindle start
- gcode += self.pausecode + "\n"
- for tool in tools:
- # Tool change sequence (optional)
- if toolchange:
- gcode += "G00 Z%.4f\n" % toolchangez
- gcode += "T%d\n" % int(tool) # Indicate tool slot (for automatic tool changer)
- gcode += "M5\n" # Spindle Stop
- gcode += "M6\n" # Tool change
- gcode += "(MSG, Change to tool dia=%.4f)\n" % exobj.tools[tool]["C"]
- gcode += "M0\n" # Temporary machine stop
- if self.spindlespeed is not None:
- gcode += "M03 S%d\n" % int(self.spindlespeed) # Spindle start with configured speed
- else:
- gcode += "M03\n" # Spindle start
- # Drillling!
- for point in points[tool]:
- x, y = point.coords.xy
- gcode += t % (x[0], y[0])
- gcode += down + up
- gcode += t % (0, 0)
- gcode += "M05\n" # Spindle stop
- self.gcode = gcode
- def generate_from_geometry_2(self,
- geometry,
- append=True,
- tooldia=None,
- tolerance=0,
- multidepth=False,
- depthpercut=None):
- """
- Second algorithm to generate from Geometry.
- ALgorithm description:
- ----------------------
- Uses RTree to find the nearest path to follow.
- :param geometry:
- :param append:
- :param tooldia:
- :param tolerance:
- :param multidepth: If True, use multiple passes to reach
- the desired depth.
- :param depthpercut: Maximum depth in each pass.
- :return: None
- """
- assert isinstance(geometry, Geometry), \
- "Expected a Geometry, got %s" % type(geometry)
- log.debug("generate_from_geometry_2()")
- ## Flatten the geometry
- # Only linear elements (no polygons) remain.
- flat_geometry = geometry.flatten(pathonly=True)
- log.debug("%d paths" % len(flat_geometry))
- ## Index first and last points in paths
- # What points to index.
- def get_pts(o):
- return [o.coords[0], o.coords[-1]]
- # Create the indexed storage.
- storage = FlatCAMRTreeStorage()
- storage.get_points = get_pts
- # Store the geometry
- log.debug("Indexing geometry before generating G-Code...")
- for shape in flat_geometry:
- if shape is not None: # TODO: This shouldn't have happened.
- storage.insert(shape)
- if tooldia is not None:
- self.tooldia = tooldia
- # self.input_geometry_bounds = geometry.bounds()
- if not append:
- self.gcode = ""
- # Initial G-Code
- self.gcode = self.unitcode[self.units.upper()] + "\n"
- self.gcode += self.absolutecode + "\n"
- self.gcode += self.feedminutecode + "\n"
- self.gcode += "F%.2f\n" % self.feedrate
- self.gcode += "G00 Z%.4f\n" % self.z_move # Move (up) to travel height
- if self.spindlespeed is not None:
- self.gcode += "M03 S%d\n" % int(self.spindlespeed) # Spindle start with configured speed
- else:
- self.gcode += "M03\n" # Spindle start
- self.gcode += self.pausecode + "\n"
- ## Iterate over geometry paths getting the nearest each time.
- log.debug("Starting G-Code...")
- path_count = 0
- current_pt = (0, 0)
- pt, geo = storage.nearest(current_pt)
- try:
- while True:
- path_count += 1
- #print "Current: ", "(%.3f, %.3f)" % current_pt
- # Remove before modifying, otherwise
- # deletion will fail.
- storage.remove(geo)
- # If last point in geometry is the nearest
- # but prefer the first one if last point == first point
- # then reverse coordinates.
- if pt != geo.coords[0] and pt == geo.coords[-1]:
- geo.coords = list(geo.coords)[::-1]
- #---------- Single depth/pass --------
- if not multidepth:
- # G-code
- # Note: self.linear2gcode() and self.point2gcode() will
- # lower and raise the tool every time.
- if type(geo) == LineString or type(geo) == LinearRing:
- self.gcode += self.linear2gcode(geo, tolerance=tolerance)
- elif type(geo) == Point:
- self.gcode += self.point2gcode(geo)
- else:
- log.warning("G-code generation not implemented for %s" % (str(type(geo))))
- #--------- Multi-pass ---------
- else:
- if isinstance(self.z_cut, Decimal):
- z_cut = self.z_cut
- else:
- z_cut = Decimal(self.z_cut).quantize(Decimal('0.000000001'))
- if depthpercut is None:
- depthpercut = z_cut
- elif not isinstance(depthpercut, Decimal):
- depthpercut = Decimal(depthpercut).quantize(Decimal('0.000000001'))
- depth = 0
- reverse = False
- while depth > z_cut:
- # Increase depth. Limit to z_cut.
- depth -= depthpercut
- if depth < z_cut:
- depth = z_cut
- # Cut at specific depth and do not lift the tool.
- # Note: linear2gcode() will use G00 to move to the
- # first point in the path, but it should be already
- # at the first point if the tool is down (in the material).
- # So, an extra G00 should show up but is inconsequential.
- if type(geo) == LineString or type(geo) == LinearRing:
- self.gcode += self.linear2gcode(geo, tolerance=tolerance,
- zcut=depth,
- up=False)
- # Ignore multi-pass for points.
- elif type(geo) == Point:
- self.gcode += self.point2gcode(geo)
- break # Ignoring ...
- else:
- log.warning("G-code generation not implemented for %s" % (str(type(geo))))
- # Reverse coordinates if not a loop so we can continue
- # cutting without returning to the beginhing.
- if type(geo) == LineString:
- geo.coords = list(geo.coords)[::-1]
- reverse = True
- # If geometry is reversed, revert.
- if reverse:
- if type(geo) == LineString:
- geo.coords = list(geo.coords)[::-1]
- # Lift the tool
- self.gcode += "G00 Z%.4f\n" % self.z_move
- # self.gcode += "( End of path. )\n"
- # Did deletion at the beginning.
- # Delete from index, update current location and continue.
- #rti.delete(hits[0], geo.coords[0])
- #rti.delete(hits[0], geo.coords[-1])
- current_pt = geo.coords[-1]
- # Next
- pt, geo = storage.nearest(current_pt)
- except StopIteration: # Nothing found in storage.
- pass
- log.debug("%s paths traced." % path_count)
- # Finish
- self.gcode += "G00 Z%.4f\n" % self.z_move # Stop cutting
- self.gcode += "G00 X0Y0\n"
- self.gcode += "M05\n" # Spindle stop
- def pre_parse(self, gtext):
- """
- Separates parts of the G-Code text into a list of dictionaries.
- Used by ``self.gcode_parse()``.
- :param gtext: A single string with g-code
- """
- # Units: G20-inches, G21-mm
- units_re = re.compile(r'^G2([01])')
- # TODO: This has to be re-done
- gcmds = []
- lines = gtext.split("\n") # TODO: This is probably a lot of work!
- for line in lines:
- # Clean up
- line = line.strip()
- # Remove comments
- # NOTE: Limited to 1 bracket pair
- op = line.find("(")
- cl = line.find(")")
- #if op > -1 and cl > op:
- if cl > op > -1:
- #comment = line[op+1:cl]
- line = line[:op] + line[(cl+1):]
- # Units
- match = units_re.match(line)
- if match:
- self.units = {'0': "IN", '1': "MM"}[match.group(1)]
- # Parse GCode
- # 0 4 12
- # G01 X-0.007 Y-0.057
- # --> codes_idx = [0, 4, 12]
- codes = "NMGXYZIJFPST"
- codes_idx = []
- i = 0
- for ch in line:
- if ch in codes:
- codes_idx.append(i)
- i += 1
- n_codes = len(codes_idx)
- if n_codes == 0:
- continue
- # Separate codes in line
- parts = []
- for p in range(n_codes - 1):
- parts.append(line[codes_idx[p]:codes_idx[p+1]].strip())
- parts.append(line[codes_idx[-1]:].strip())
- # Separate codes from values
- cmds = {}
- for part in parts:
- cmds[part[0]] = float(part[1:])
- gcmds.append(cmds)
- return gcmds
- def gcode_parse(self):
- """
- G-Code parser (from self.gcode). Generates dictionary with
- single-segment LineString's and "kind" indicating cut or travel,
- fast or feedrate speed.
- """
- kind = ["C", "F"] # T=travel, C=cut, F=fast, S=slow
- # Results go here
- geometry = []
-
- # TODO: Merge into single parser?
- gobjs = self.pre_parse(self.gcode)
-
- # Last known instruction
- current = {'X': 0.0, 'Y': 0.0, 'Z': 0.0, 'G': 0}
- # Current path: temporary storage until tool is
- # lifted or lowered.
- path = [(0, 0)]
- # Process every instruction
- for gobj in gobjs:
- ## Changing height
- if 'Z' in gobj:
- if ('X' in gobj or 'Y' in gobj) and gobj['Z'] != current['Z']:
- log.warning("Non-orthogonal motion: From %s" % str(current))
- log.warning(" To: %s" % str(gobj))
- current['Z'] = gobj['Z']
- # Store the path into geometry and reset path
- if len(path) > 1:
- geometry.append({"geom": LineString(path),
- "kind": kind})
- path = [path[-1]] # Start with the last point of last path.
- if 'G' in gobj:
- current['G'] = int(gobj['G'])
-
- if 'X' in gobj or 'Y' in gobj:
-
- if 'X' in gobj:
- x = gobj['X']
- else:
- x = current['X']
-
- if 'Y' in gobj:
- y = gobj['Y']
- else:
- y = current['Y']
- kind = ["C", "F"] # T=travel, C=cut, F=fast, S=slow
- if current['Z'] > 0:
- kind[0] = 'T'
- if current['G'] > 0:
- kind[1] = 'S'
-
- arcdir = [None, None, "cw", "ccw"]
- if current['G'] in [0, 1]: # line
- path.append((x, y))
- if current['G'] in [2, 3]: # arc
- center = [gobj['I'] + current['X'], gobj['J'] + current['Y']]
- radius = sqrt(gobj['I']**2 + gobj['J']**2)
- start = arctan2(-gobj['J'], -gobj['I'])
- stop = arctan2(-center[1]+y, -center[0]+x)
- path += arc(center, radius, start, stop,
- arcdir[current['G']],
- self.steps_per_circ)
- # Update current instruction
- for code in gobj:
- current[code] = gobj[code]
- # There might not be a change in height at the
- # end, therefore, see here too if there is
- # a final path.
- if len(path) > 1:
- geometry.append({"geom": LineString(path),
- "kind": kind})
- self.gcode_parsed = geometry
- return geometry
- # def plot(self, tooldia=None, dpi=75, margin=0.1,
- # color={"T": ["#F0E24D", "#B5AB3A"], "C": ["#5E6CFF", "#4650BD"]},
- # alpha={"T": 0.3, "C": 1.0}):
- # """
- # Creates a Matplotlib figure with a plot of the
- # G-code job.
- # """
- # if tooldia is None:
- # tooldia = self.tooldia
- #
- # fig = Figure(dpi=dpi)
- # ax = fig.add_subplot(111)
- # ax.set_aspect(1)
- # xmin, ymin, xmax, ymax = self.input_geometry_bounds
- # ax.set_xlim(xmin-margin, xmax+margin)
- # ax.set_ylim(ymin-margin, ymax+margin)
- #
- # if tooldia == 0:
- # for geo in self.gcode_parsed:
- # linespec = '--'
- # linecolor = color[geo['kind'][0]][1]
- # if geo['kind'][0] == 'C':
- # linespec = 'k-'
- # x, y = geo['geom'].coords.xy
- # ax.plot(x, y, linespec, color=linecolor)
- # else:
- # for geo in self.gcode_parsed:
- # poly = geo['geom'].buffer(tooldia/2.0)
- # patch = PolygonPatch(poly, facecolor=color[geo['kind'][0]][0],
- # edgecolor=color[geo['kind'][0]][1],
- # alpha=alpha[geo['kind'][0]], zorder=2)
- # ax.add_patch(patch)
- #
- # return fig
-
- def plot2(self, axes, tooldia=None, dpi=75, margin=0.1,
- color={"T": ["#F0E24D", "#B5AB3A"], "C": ["#5E6CFF", "#4650BD"]},
- alpha={"T": 0.3, "C": 1.0}, tool_tolerance=0.0005):
- """
- Plots the G-code job onto the given axes.
- :param axes: Matplotlib axes on which to plot.
- :param tooldia: Tool diameter.
- :param dpi: Not used!
- :param margin: Not used!
- :param color: Color specification.
- :param alpha: Transparency specification.
- :param tool_tolerance: Tolerance when drawing the toolshape.
- :return: None
- """
- path_num = 0
- if tooldia is None:
- tooldia = self.tooldia
-
- if tooldia == 0:
- for geo in self.gcode_parsed:
- linespec = '--'
- linecolor = color[geo['kind'][0]][1]
- if geo['kind'][0] == 'C':
- linespec = 'k-'
- x, y = geo['geom'].coords.xy
- axes.plot(x, y, linespec, color=linecolor)
- else:
- for geo in self.gcode_parsed:
- path_num += 1
- axes.annotate(str(path_num), xy=geo['geom'].coords[0],
- xycoords='data')
- poly = geo['geom'].buffer(tooldia / 2.0).simplify(tool_tolerance)
- patch = PolygonPatch(poly, facecolor=color[geo['kind'][0]][0],
- edgecolor=color[geo['kind'][0]][1],
- alpha=alpha[geo['kind'][0]], zorder=2)
- axes.add_patch(patch)
-
- def create_geometry(self):
- # TODO: This takes forever. Too much data?
- self.solid_geometry = cascaded_union([geo['geom'] for geo in self.gcode_parsed])
- def linear2gcode(self, linear, tolerance=0, down=True, up=True,
- zcut=None, ztravel=None, downrate=None,
- feedrate=None, cont=False):
- """
- Generates G-code to cut along the linear feature.
- :param linear: The path to cut along.
- :type: Shapely.LinearRing or Shapely.Linear String
- :param tolerance: All points in the simplified object will be within the
- tolerance distance of the original geometry.
- :type tolerance: float
- :return: G-code to cut along the linear feature.
- :rtype: str
- """
- if zcut is None:
- zcut = self.z_cut
- if ztravel is None:
- ztravel = self.z_move
- if downrate is None:
- downrate = self.zdownrate
- if feedrate is None:
- feedrate = self.feedrate
- t = "G0%d " + CNCjob.defaults["coordinate_format"] + "\n"
- # Simplify paths?
- if tolerance > 0:
- target_linear = linear.simplify(tolerance)
- else:
- target_linear = linear
- gcode = ""
- path = list(target_linear.coords)
- # Move fast to 1st point
- if not cont:
- gcode += t % (0, path[0][0], path[0][1]) # Move to first point
- # Move down to cutting depth
- if down:
- # Different feedrate for vertical cut?
- if self.zdownrate is not None:
- gcode += "F%.2f\n" % downrate
- gcode += "G01 Z%.4f\n" % zcut # Start cutting
- gcode += "F%.2f\n" % feedrate # Restore feedrate
- else:
- gcode += "G01 Z%.4f\n" % zcut # Start cutting
- # Cutting...
- for pt in path[1:]:
- gcode += t % (1, pt[0], pt[1]) # Linear motion to point
- # Up to travelling height.
- if up:
- gcode += "G00 Z%.4f\n" % ztravel # Stop cutting
- return gcode
- def point2gcode(self, point):
- gcode = ""
- #t = "G0%d X%.4fY%.4f\n"
- t = "G0%d " + CNCjob.defaults["coordinate_format"] + "\n"
- path = list(point.coords)
- gcode += t % (0, path[0][0], path[0][1]) # Move to first point
- if self.zdownrate is not None:
- gcode += "F%.2f\n" % self.zdownrate
- gcode += "G01 Z%.4f\n" % self.z_cut # Start cutting
- gcode += "F%.2f\n" % self.feedrate
- else:
- gcode += "G01 Z%.4f\n" % self.z_cut # Start cutting
- gcode += "G00 Z%.4f\n" % self.z_move # Stop cutting
- return gcode
- def scale(self, factor):
- """
- Scales all the geometry on the XY plane in the object by the
- given factor. Tool sizes, feedrates, or Z-axis dimensions are
- not altered.
- :param factor: Number by which to scale the object.
- :type factor: float
- :return: None
- :rtype: None
- """
- for g in self.gcode_parsed:
- g['geom'] = affinity.scale(g['geom'], factor, factor, origin=(0, 0))
- self.create_geometry()
- def offset(self, vect):
- """
- Offsets all the geometry on the XY plane in the object by the
- given vector.
- :param vect: (x, y) offset vector.
- :type vect: tuple
- :return: None
- """
- dx, dy = vect
- for g in self.gcode_parsed:
- g['geom'] = affinity.translate(g['geom'], xoff=dx, yoff=dy)
- self.create_geometry()
- # def get_bounds(geometry_set):
- # xmin = Inf
- # ymin = Inf
- # xmax = -Inf
- # ymax = -Inf
- #
- # #print "Getting bounds of:", str(geometry_set)
- # for gs in geometry_set:
- # try:
- # gxmin, gymin, gxmax, gymax = geometry_set[gs].bounds()
- # xmin = min([xmin, gxmin])
- # ymin = min([ymin, gymin])
- # xmax = max([xmax, gxmax])
- # ymax = max([ymax, gymax])
- # except:
- # print "DEV WARNING: Tried to get bounds of empty geometry."
- #
- # return [xmin, ymin, xmax, ymax]
- def get_bounds(geometry_list):
- xmin = Inf
- ymin = Inf
- xmax = -Inf
- ymax = -Inf
- #print "Getting bounds of:", str(geometry_set)
- for gs in geometry_list:
- try:
- gxmin, gymin, gxmax, gymax = gs.bounds()
- xmin = min([xmin, gxmin])
- ymin = min([ymin, gymin])
- xmax = max([xmax, gxmax])
- ymax = max([ymax, gymax])
- except:
- log.warning("DEVELOPMENT: Tried to get bounds of empty geometry.")
- return [xmin, ymin, xmax, ymax]
- def arc(center, radius, start, stop, direction, steps_per_circ):
- """
- Creates a list of point along the specified arc.
- :param center: Coordinates of the center [x, y]
- :type center: list
- :param radius: Radius of the arc.
- :type radius: float
- :param start: Starting angle in radians
- :type start: float
- :param stop: End angle in radians
- :type stop: float
- :param direction: Orientation of the arc, "CW" or "CCW"
- :type direction: string
- :param steps_per_circ: Number of straight line segments to
- represent a circle.
- :type steps_per_circ: int
- :return: The desired arc, as list of tuples
- :rtype: list
- """
- # TODO: Resolution should be established by maximum error from the exact arc.
- da_sign = {"cw": -1.0, "ccw": 1.0}
- points = []
- if direction == "ccw" and stop <= start:
- stop += 2 * pi
- if direction == "cw" and stop >= start:
- stop -= 2 * pi
-
- angle = abs(stop - start)
-
- #angle = stop-start
- steps = max([int(ceil(angle / (2 * pi) * steps_per_circ)), 2])
- delta_angle = da_sign[direction] * angle * 1.0 / steps
- for i in range(steps + 1):
- theta = start + delta_angle * i
- points.append((center[0] + radius * cos(theta), center[1] + radius * sin(theta)))
- return points
- def arc2(p1, p2, center, direction, steps_per_circ):
- r = sqrt((center[0] - p1[0]) ** 2 + (center[1] - p1[1]) ** 2)
- start = arctan2(p1[1] - center[1], p1[0] - center[0])
- stop = arctan2(p2[1] - center[1], p2[0] - center[0])
- return arc(center, r, start, stop, direction, steps_per_circ)
- def arc_angle(start, stop, direction):
- if direction == "ccw" and stop <= start:
- stop += 2 * pi
- if direction == "cw" and stop >= start:
- stop -= 2 * pi
- angle = abs(stop - start)
- return angle
- # def find_polygon(poly, point):
- # """
- # Find an object that object.contains(Point(point)) in
- # poly, which can can be iterable, contain iterable of, or
- # be itself an implementer of .contains().
- #
- # :param poly: See description
- # :return: Polygon containing point or None.
- # """
- #
- # if poly is None:
- # return None
- #
- # try:
- # for sub_poly in poly:
- # p = find_polygon(sub_poly, point)
- # if p is not None:
- # return p
- # except TypeError:
- # try:
- # if poly.contains(Point(point)):
- # return poly
- # except AttributeError:
- # return None
- #
- # return None
- def to_dict(obj):
- """
- Makes the following types into serializable form:
- * ApertureMacro
- * BaseGeometry
- :param obj: Shapely geometry.
- :type obj: BaseGeometry
- :return: Dictionary with serializable form if ``obj`` was
- BaseGeometry or ApertureMacro, otherwise returns ``obj``.
- """
- if isinstance(obj, ApertureMacro):
- return {
- "__class__": "ApertureMacro",
- "__inst__": obj.to_dict()
- }
- if isinstance(obj, BaseGeometry):
- return {
- "__class__": "Shply",
- "__inst__": sdumps(obj)
- }
- return obj
- def dict2obj(d):
- """
- Default deserializer.
- :param d: Serializable dictionary representation of an object
- to be reconstructed.
- :return: Reconstructed object.
- """
- if '__class__' in d and '__inst__' in d:
- if d['__class__'] == "Shply":
- return sloads(d['__inst__'])
- if d['__class__'] == "ApertureMacro":
- am = ApertureMacro()
- am.from_dict(d['__inst__'])
- return am
- return d
- else:
- return d
- def plotg(geo, solid_poly=False, color="black"):
- try:
- _ = iter(geo)
- except:
- geo = [geo]
- for g in geo:
- if type(g) == Polygon:
- if solid_poly:
- patch = PolygonPatch(g,
- facecolor="#BBF268",
- edgecolor="#006E20",
- alpha=0.75,
- zorder=2)
- ax = subplot(111)
- ax.add_patch(patch)
- else:
- x, y = g.exterior.coords.xy
- plot(x, y, color=color)
- for ints in g.interiors:
- x, y = ints.coords.xy
- plot(x, y, color=color)
- continue
- if type(g) == LineString or type(g) == LinearRing:
- x, y = g.coords.xy
- plot(x, y, color=color)
- continue
- if type(g) == Point:
- x, y = g.coords.xy
- plot(x, y, 'o')
- continue
- try:
- _ = iter(g)
- plotg(g, color=color)
- except:
- log.error("Cannot plot: " + str(type(g)))
- continue
- def parse_gerber_number(strnumber, frac_digits):
- """
- Parse a single number of Gerber coordinates.
- :param strnumber: String containing a number in decimal digits
- from a coordinate data block, possibly with a leading sign.
- :type strnumber: str
- :param frac_digits: Number of digits used for the fractional
- part of the number
- :type frac_digits: int
- :return: The number in floating point.
- :rtype: float
- """
- return int(strnumber) * (10 ** (-frac_digits))
- # def voronoi(P):
- # """
- # Returns a list of all edges of the voronoi diagram for the given input points.
- # """
- # delauny = Delaunay(P)
- # triangles = delauny.points[delauny.vertices]
- #
- # circum_centers = np.array([triangle_csc(tri) for tri in triangles])
- # long_lines_endpoints = []
- #
- # lineIndices = []
- # for i, triangle in enumerate(triangles):
- # circum_center = circum_centers[i]
- # for j, neighbor in enumerate(delauny.neighbors[i]):
- # if neighbor != -1:
- # lineIndices.append((i, neighbor))
- # else:
- # ps = triangle[(j+1)%3] - triangle[(j-1)%3]
- # ps = np.array((ps[1], -ps[0]))
- #
- # middle = (triangle[(j+1)%3] + triangle[(j-1)%3]) * 0.5
- # di = middle - triangle[j]
- #
- # ps /= np.linalg.norm(ps)
- # di /= np.linalg.norm(di)
- #
- # if np.dot(di, ps) < 0.0:
- # ps *= -1000.0
- # else:
- # ps *= 1000.0
- #
- # long_lines_endpoints.append(circum_center + ps)
- # lineIndices.append((i, len(circum_centers) + len(long_lines_endpoints)-1))
- #
- # vertices = np.vstack((circum_centers, long_lines_endpoints))
- #
- # # filter out any duplicate lines
- # lineIndicesSorted = np.sort(lineIndices) # make (1,2) and (2,1) both (1,2)
- # lineIndicesTupled = [tuple(row) for row in lineIndicesSorted]
- # lineIndicesUnique = np.unique(lineIndicesTupled)
- #
- # return vertices, lineIndicesUnique
- #
- #
- # def triangle_csc(pts):
- # rows, cols = pts.shape
- #
- # A = np.bmat([[2 * np.dot(pts, pts.T), np.ones((rows, 1))],
- # [np.ones((1, rows)), np.zeros((1, 1))]])
- #
- # b = np.hstack((np.sum(pts * pts, axis=1), np.ones((1))))
- # x = np.linalg.solve(A,b)
- # bary_coords = x[:-1]
- # return np.sum(pts * np.tile(bary_coords.reshape((pts.shape[0], 1)), (1, pts.shape[1])), axis=0)
- #
- #
- # def voronoi_cell_lines(points, vertices, lineIndices):
- # """
- # Returns a mapping from a voronoi cell to its edges.
- #
- # :param points: shape (m,2)
- # :param vertices: shape (n,2)
- # :param lineIndices: shape (o,2)
- # :rtype: dict point index -> list of shape (n,2) with vertex indices
- # """
- # kd = KDTree(points)
- #
- # cells = collections.defaultdict(list)
- # for i1, i2 in lineIndices:
- # v1, v2 = vertices[i1], vertices[i2]
- # mid = (v1+v2)/2
- # _, (p1Idx, p2Idx) = kd.query(mid, 2)
- # cells[p1Idx].append((i1, i2))
- # cells[p2Idx].append((i1, i2))
- #
- # return cells
- #
- #
- # def voronoi_edges2polygons(cells):
- # """
- # Transforms cell edges into polygons.
- #
- # :param cells: as returned from voronoi_cell_lines
- # :rtype: dict point index -> list of vertex indices which form a polygon
- # """
- #
- # # first, close the outer cells
- # for pIdx, lineIndices_ in cells.items():
- # dangling_lines = []
- # for i1, i2 in lineIndices_:
- # connections = filter(lambda (i1_, i2_): (i1, i2) != (i1_, i2_) and (i1 == i1_ or i1 == i2_ or i2 == i1_ or i2 == i2_), lineIndices_)
- # assert 1 <= len(connections) <= 2
- # if len(connections) == 1:
- # dangling_lines.append((i1, i2))
- # assert len(dangling_lines) in [0, 2]
- # if len(dangling_lines) == 2:
- # (i11, i12), (i21, i22) = dangling_lines
- #
- # # determine which line ends are unconnected
- # connected = filter(lambda (i1,i2): (i1,i2) != (i11,i12) and (i1 == i11 or i2 == i11), lineIndices_)
- # i11Unconnected = len(connected) == 0
- #
- # connected = filter(lambda (i1,i2): (i1,i2) != (i21,i22) and (i1 == i21 or i2 == i21), lineIndices_)
- # i21Unconnected = len(connected) == 0
- #
- # startIdx = i11 if i11Unconnected else i12
- # endIdx = i21 if i21Unconnected else i22
- #
- # cells[pIdx].append((startIdx, endIdx))
- #
- # # then, form polygons by storing vertex indices in (counter-)clockwise order
- # polys = dict()
- # for pIdx, lineIndices_ in cells.items():
- # # get a directed graph which contains both directions and arbitrarily follow one of both
- # directedGraph = lineIndices_ + [(i2, i1) for (i1, i2) in lineIndices_]
- # directedGraphMap = collections.defaultdict(list)
- # for (i1, i2) in directedGraph:
- # directedGraphMap[i1].append(i2)
- # orderedEdges = []
- # currentEdge = directedGraph[0]
- # while len(orderedEdges) < len(lineIndices_):
- # i1 = currentEdge[1]
- # i2 = directedGraphMap[i1][0] if directedGraphMap[i1][0] != currentEdge[0] else directedGraphMap[i1][1]
- # nextEdge = (i1, i2)
- # orderedEdges.append(nextEdge)
- # currentEdge = nextEdge
- #
- # polys[pIdx] = [i1 for (i1, i2) in orderedEdges]
- #
- # return polys
- #
- #
- # def voronoi_polygons(points):
- # """
- # Returns the voronoi polygon for each input point.
- #
- # :param points: shape (n,2)
- # :rtype: list of n polygons where each polygon is an array of vertices
- # """
- # vertices, lineIndices = voronoi(points)
- # cells = voronoi_cell_lines(points, vertices, lineIndices)
- # polys = voronoi_edges2polygons(cells)
- # polylist = []
- # for i in xrange(len(points)):
- # poly = vertices[np.asarray(polys[i])]
- # polylist.append(poly)
- # return polylist
- #
- #
- # class Zprofile:
- # def __init__(self):
- #
- # # data contains lists of [x, y, z]
- # self.data = []
- #
- # # Computed voronoi polygons (shapely)
- # self.polygons = []
- # pass
- #
- # def plot_polygons(self):
- # axes = plt.subplot(1, 1, 1)
- #
- # plt.axis([-0.05, 1.05, -0.05, 1.05])
- #
- # for poly in self.polygons:
- # p = PolygonPatch(poly, facecolor=np.random.rand(3, 1), alpha=0.3)
- # axes.add_patch(p)
- #
- # def init_from_csv(self, filename):
- # pass
- #
- # def init_from_string(self, zpstring):
- # pass
- #
- # def init_from_list(self, zplist):
- # self.data = zplist
- #
- # def generate_polygons(self):
- # self.polygons = [Polygon(p) for p in voronoi_polygons(array([[x[0], x[1]] for x in self.data]))]
- #
- # def normalize(self, origin):
- # pass
- #
- # def paste(self, path):
- # """
- # Return a list of dictionaries containing the parts of the original
- # path and their z-axis offset.
- # """
- #
- # # At most one region/polygon will contain the path
- # containing = [i for i in range(len(self.polygons)) if self.polygons[i].contains(path)]
- #
- # if len(containing) > 0:
- # return [{"path": path, "z": self.data[containing[0]][2]}]
- #
- # # All region indexes that intersect with the path
- # crossing = [i for i in range(len(self.polygons)) if self.polygons[i].intersects(path)]
- #
- # return [{"path": path.intersection(self.polygons[i]),
- # "z": self.data[i][2]} for i in crossing]
- def autolist(obj):
- try:
- _ = iter(obj)
- return obj
- except TypeError:
- return [obj]
- def three_point_circle(p1, p2, p3):
- """
- Computes the center and radius of a circle from
- 3 points on its circumference.
- :param p1: Point 1
- :param p2: Point 2
- :param p3: Point 3
- :return: center, radius
- """
- # Midpoints
- a1 = (p1 + p2) / 2.0
- a2 = (p2 + p3) / 2.0
- # Normals
- b1 = dot((p2 - p1), array([[0, -1], [1, 0]], dtype=float32))
- b2 = dot((p3 - p2), array([[0, 1], [-1, 0]], dtype=float32))
- # Params
- T = solve(transpose(array([-b1, b2])), a1 - a2)
- # Center
- center = a1 + b1 * T[0]
- # Radius
- radius = norm(center - p1)
- return center, radius, T[0]
- def distance(pt1, pt2):
- return sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2)
- class FlatCAMRTree(object):
- def __init__(self):
- # Python RTree Index
- self.rti = rtindex.Index()
- ## Track object-point relationship
- # Each is list of points in object.
- self.obj2points = []
- # Index is index in rtree, value is index of
- # object in obj2points.
- self.points2obj = []
- self.get_points = lambda go: go.coords
- def grow_obj2points(self, idx):
- """
- Increases the size of self.obj2points to fit
- idx + 1 items.
- :param idx: Index to fit into list.
- :return: None
- """
- if len(self.obj2points) > idx:
- # len == 2, idx == 1, ok.
- return
- else:
- # len == 2, idx == 2, need 1 more.
- # range(2, 3)
- for i in range(len(self.obj2points), idx + 1):
- self.obj2points.append([])
- def insert(self, objid, obj):
- self.grow_obj2points(objid)
- self.obj2points[objid] = []
- for pt in self.get_points(obj):
- self.rti.insert(len(self.points2obj), (pt[0], pt[1], pt[0], pt[1]), obj=objid)
- self.obj2points[objid].append(len(self.points2obj))
- self.points2obj.append(objid)
- def remove_obj(self, objid, obj):
- # Use all ptids to delete from index
- for i, pt in enumerate(self.get_points(obj)):
- self.rti.delete(self.obj2points[objid][i], (pt[0], pt[1], pt[0], pt[1]))
- def nearest(self, pt):
- """
- Will raise StopIteration if no items are found.
- :param pt:
- :return:
- """
- return self.rti.nearest(pt, objects=True).next()
- class FlatCAMRTreeStorage(FlatCAMRTree):
- def __init__(self):
- super(FlatCAMRTreeStorage, self).__init__()
- self.objects = []
- # Optimization attempt!
- self.indexes = {}
- def insert(self, obj):
- self.objects.append(obj)
- idx = len(self.objects) - 1
- # Note: Shapely objects are not hashable any more, althought
- # there seem to be plans to re-introduce the feature in
- # version 2.0. For now, we will index using the object's id,
- # but it's important to remember that shapely geometry is
- # mutable, ie. it can be modified to a totally different shape
- # and continue to have the same id.
- # self.indexes[obj] = idx
- self.indexes[id(obj)] = idx
- super(FlatCAMRTreeStorage, self).insert(idx, obj)
- #@profile
- def remove(self, obj):
- # See note about self.indexes in insert().
- # objidx = self.indexes[obj]
- objidx = self.indexes[id(obj)]
- # Remove from list
- self.objects[objidx] = None
- # Remove from index
- self.remove_obj(objidx, obj)
- def get_objects(self):
- return (o for o in self.objects if o is not None)
- def nearest(self, pt):
- """
- Returns the nearest matching points and the object
- it belongs to.
- :param pt: Query point.
- :return: (match_x, match_y), Object owner of
- matching point.
- :rtype: tuple
- """
- tidx = super(FlatCAMRTreeStorage, self).nearest(pt)
- return (tidx.bbox[0], tidx.bbox[1]), self.objects[tidx.object]
- # class myO:
- # def __init__(self, coords):
- # self.coords = coords
- #
- #
- # def test_rti():
- #
- # o1 = myO([(0, 0), (0, 1), (1, 1)])
- # o2 = myO([(2, 0), (2, 1), (2, 1)])
- # o3 = myO([(2, 0), (2, 1), (3, 1)])
- #
- # os = [o1, o2]
- #
- # idx = FlatCAMRTree()
- #
- # for o in range(len(os)):
- # idx.insert(o, os[o])
- #
- # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]
- #
- # idx.remove_obj(0, o1)
- #
- # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]
- #
- # idx.remove_obj(1, o2)
- #
- # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]
- #
- #
- # def test_rtis():
- #
- # o1 = myO([(0, 0), (0, 1), (1, 1)])
- # o2 = myO([(2, 0), (2, 1), (2, 1)])
- # o3 = myO([(2, 0), (2, 1), (3, 1)])
- #
- # os = [o1, o2]
- #
- # idx = FlatCAMRTreeStorage()
- #
- # for o in range(len(os)):
- # idx.insert(os[o])
- #
- # #os = None
- # #o1 = None
- # #o2 = None
- #
- # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]
- #
- # idx.remove(idx.nearest((2,0))[1])
- #
- # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]
- #
- # idx.remove(idx.nearest((0,0))[1])
- #
- # print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)]
|