diff options
Diffstat (limited to 'kig/pykig')
-rw-r--r-- | kig/pykig/API.txt | 182 | ||||
-rw-r--r-- | kig/pykig/Makefile.am | 1 | ||||
-rw-r--r-- | kig/pykig/VERSION | 1 | ||||
-rw-r--r-- | kig/pykig/changelog.txt | 64 | ||||
-rw-r--r-- | kig/pykig/pykig.pth | 2 | ||||
-rwxr-xr-x | kig/pykig/pykig.py | 824 |
6 files changed, 1074 insertions, 0 deletions
diff --git a/kig/pykig/API.txt b/kig/pykig/API.txt new file mode 100644 index 00000000..e775ac76 --- /dev/null +++ b/kig/pykig/API.txt @@ -0,0 +1,182 @@ +Version 0.2.4 + +There are two way to use pykig.py: +- as a program: + $ pykig.py <nameprog>.kpy +- as a Python library within a Python program: + from pykig import * + +A ".kpy" file is a "python" script. + +A new object is created by callig a python "constructor"; +the result can be stored in a python variable for later +use. E.g.: + + a = Point(0.5, 1.5, name="A") + +to create a point with coordinates (0.5, 1.5), give it a +the name "A" (at kig level) and recall it in the python +variable a. See the examples for practical use. + +All constructors accept some optional parameters: + +shown = HIDDEN | VISIBLE default value: VISIBLE +name = string a name to refer to the object within + kig +internal = bool this object is internal and cannot be + made visible +width = integer the line width +pointstyle = "Round"|"RoundEmpty"|"Rectangular"|"RectangularEmpty"|"Cross" +linestyle = "SolidLine"|"DashLine"|"DashDotLine"|"DashDotDotLine"|"DotLine" +color = "#RRGGBB" where RR, GG, BB are three numbers + wich represent the red, green, blue + components + +The kigdocument is a global object to allow two methods for modify Kig look and +default behaviour: + +kigdocument.noaxes() +kigdocument.nogrid() +kigdocument.hideobjects() +kigdocument.showobjects() +kigdocument.setwidth() +kigdocument.setpointstyle() +kigdocument.setname() +kigdocument.setlinestyle() +kigdocument.setshown() +kigdocument.setcolor() +kigdocument.setinternal() + + +Generic methods for objects: + +obj.hide() +obj.show() hide/show given object +obj.setwidth(width) set obj width +obj.setpointstyle(self, pointstyle) set obj point style +obj.setlinestyle(self, linestyle) set obj line style +obj.setcolor(self, color) set obj color +obj.setname() set obj name +obj.type() return obj type + +Some objects have other methods: + +obj.coordinate() for points +obj.xcoord() +obj.ycoord() +obj.midpoint() for segments +obj.endpointA() +obj.endpointB() +obj.length() +obj.equation() +obj.slope() +obj.numofsides() for polygons +obj.perimeter() +obj.surface() +obj.centerofmass() +obj.windingnumber() +obj.center() for circles +obj.bisector() for angles +obj.support() + +==================================================================== + +Properties: + +Type(object) type of object +Coordinate(point) coordinate of point +XCoord(point) +YCoord(point) +MidPoints(a, b) midpoint of two points a and b +MidPoint(segment) midpoint of a segment +EndPointA(segment) +EndPointB(segment) +Length(segment) +Equation(segment) +Slope(segment) +NumOfSides(poly) +Perimeter(poly) +Surface(poly) +CenterOfMass(poly) +WindingNumber(poly) +Center(circle) +Bisector(angle) +Support(object) +==================================================================== + +Objects: + +Point(x, y) free (unconstrained) point +ConstrainedPoint(t, curve) constrained point on 'curve'; t + *must* be in [0,1]; for segments and + arcs the position of the point is + proportional to the value of t +Line(a, b) straight line through a and b +Ray(a, b) halfline starting in a through b +Segment(a, b) segment from a to b +Orthogonal(line, point) line through 'point' orthogonal to + 'line' + +Circle(center, point) +CircleByCenterRadius(center, radius) +CircleBy3Points(p1, p2, p3) +ArcBy3Points(p1, p2, p3) +ArcByCenterPointAngle(center, point, angle) + +ParabolaByDirectrixFocus(directrix, focus) +VerticalCubic(p1, p2, p3, p4) +ConicArc(p1, p2, p3, center) + +LineLineIntersection(line1, line2) produces a point +CircleCircleIntersection(c1, c2, which) c1 and c2 are two circles, 'which' + is an integer that can only be +1 + or -1 and tells which one of the + two intersections has to be created. + To have both you must call this + function twice. +ConicLineIntersection(conic, line, which) conic can also be a circle; which + has the same meaning as for the + CircleCircleIntersection + +Polygon((p1, p2,..., pn)) A polygon with the given vertices +PolygonBCV(center, vertex, n) A regular polygon with 'n' sides +PolygonVertex(polygon, i) Generate the i-th vertex of the + given polygon +PolygonSide(polygon, i) Generate the i-th side of the given + polygon + +Vector(p1, p2) +Angle(p1, center, p2) + +Text(point, string, boxed) point is a Point or a tuple of + two numbers + boxed is a integer in [0, 1] telling + if we want the frame +VarText(point, string, vars, boxed) point is a Point or a tuple of + two numbers + vars hold variables parts +Label(obj, displ, string, boxed) is a Text connected tu a object +VarLabel(obj, displ, string, vars, boxed) + +PythonScript(script, argvec) "script" is a string containing the + python script + +Translate(object, vector) The result is an object of the same + kind as 'object' +CentralSymmetry(object, center) +AxialSymmetry(object, line) +Rotate(object,center, angle) +Scale(object,center, segment) The length of the segment is the + scaling ratio +Scale2(object, center, s1, s2) The ratio of the lengths of s1 and + s2 is the scaling ratio +InvertPoint(point, circle) Circular invertion of: a point +InvertLine(line, circle) ... an other object +InvertCircle(circle, circle) +InvertArc(arc, circle) +InvertSegment(segment, circle) +CircularInversion(object, circle) + +------------------------------------------------------------------------------- + + diff --git a/kig/pykig/Makefile.am b/kig/pykig/Makefile.am new file mode 100644 index 00000000..40402a36 --- /dev/null +++ b/kig/pykig/Makefile.am @@ -0,0 +1 @@ +bin_SCRIPTS = pykig.py diff --git a/kig/pykig/VERSION b/kig/pykig/VERSION new file mode 100644 index 00000000..d3b5ba4b --- /dev/null +++ b/kig/pykig/VERSION @@ -0,0 +1 @@ +0.2.11 diff --git a/kig/pykig/changelog.txt b/kig/pykig/changelog.txt new file mode 100644 index 00000000..94c604c0 --- /dev/null +++ b/kig/pykig/changelog.txt @@ -0,0 +1,64 @@ +0.2.10 +Aggiunte alcune virgole mancanti +cambiato il nome dell'oggetti da InvertObject a CircularInversion +0.2.9 +Eliminati gli oggetti: InvertLine, InvertCircle, InvertArc, InvertSegment +Aggiunto gli oggetti InvertObject, VerticalCubic, ConicArc +Aggiunta la proproietà support +Modificati alcuni esempi +0.2.8 +Aggiunte a Triangle le proprietà dei poligoni. +0.2.7 +Aggiunta, al manuale, la sezione sui metodi di kigdocument. +E sugli oggetti Punto, Linea, Circonferenza, Parabola. +Corretto un bug sul valore di default di internal. +0.2.6 +Aggiustata la codifica dei caratteri nel manuale. +0.2.5 +Scritti i primi 3 capitoli del manuale. +Modificato gli esempi in modo che funzionino. +7 nuovi metodi in KigDocument per settare i valori di default. +eliminate la funzioni per settare i valori di default- +Semplificata la gerarchia di classi con l'eliminazione di KigOut. +0.2.4 +Aggiunti: gli oggetti "InvertArc" e "InvertSegment" +Predisposte le tuple per l'aggiunta di propietà di alcuni oggetti. +0.2.3 +Corretta la generazione del file .kig +Aggiunta la proprietà "Bisector" +Aggiunto il metodo "bisector" +Aggiunti 4 nuovi esempi. +Nell'esempio 1170Circocentro2.kpy è inserito un esempio di OOP + in 3 righe di codice: + la cerazione della classe Segmento che estende la classe "Segment" +0.2.2 +Modificato il nome del programma: da pykig a pykig.py +Modificato il nome della funzione main() ora si chiama prog() +Modificato l'impianto del programma in modo che possa essere eseguito sia + come programma, sia come libreria. +In particolare: modificati i metodi di KigDocument: __init__() e close() +Inseriti dei controlli sugli errori in scrittura del file +Aggiunti gli esempi esempi01, equivalenti a quelli di esempi00, che + utilizzano pykig.py come libreria +Rinominata la directory con gli esempi seri in esempi02. +Rimodificato il comportamento di pykig.py, quando si chiede di avere un file + in output non chiama Kig. +Ora le stringhe visualizzano correttamente le lettere accentate. +Modificata la funzione convstr(s), aggiunta la costante DICT. +Aggiunto l'oggetto Label che associa una stringa ad un oggetto. +Aggiornato il file API.txt per allinearlo ai cambiamenti. +0.2.1: +Modificato il numero di versione, per renderlo coerente tra codice e nome +del file compresso. +Modificata la gerarchia di classi con l'aggiunta di KigOut +Modificati i nomi di variabili che hanno visibilità all'interno dei file .pyk +Riunito tutte le istruzioni di scrittura sul file in un'unica procedura: + KigDocument.close() +Resa inutile la chiamata a kigopen() +Aggiunte le due funzioni noaxes() e nogrid() +Riuniti una sequenza di comandi nella funzione main() che viene eseguita + quando pykig viene chiamato come programma. +Modificata la costruzione di alcuni oggetti: dove è richiesto un punto di tipo + internal si può passare una tupla con le due coordinate. +Modificati gli esempi in modo da farli funzionere con le nuove + caratteristiche. diff --git a/kig/pykig/pykig.pth b/kig/pykig/pykig.pth new file mode 100644 index 00000000..203b81d4 --- /dev/null +++ b/kig/pykig/pykig.pth @@ -0,0 +1,2 @@ +# +pykig diff --git a/kig/pykig/pykig.py b/kig/pykig/pykig.py new file mode 100755 index 00000000..106c8a0c --- /dev/null +++ b/kig/pykig/pykig.py @@ -0,0 +1,824 @@ +#!/usr/bin/env python +# -*- coding: iso-8859-15 -*- +#-------------------------------python------------------------pykig.py--# +# # +# Da Python a Kig # +# # +#--Maurizio Paolini-Daniele Zambelli-----------------------------2005---# +# +# (licenza GPL) + +version="0.2.11" + +##### +# Type constant +##### +TI=type(0) +TF=type(0.) +TS=type("") +TT=type((0, 0)) + +##### +# Constants: Point Style, Line Style, defaults values... +##### +PS=("Round", "RoundEmpty", "Rectangular", "RectangularEmpty", "Cross") +LS=("SolidLine", "DashLine", "DashDotLine", "DashDotDotLine", "DotLine") +KIGTRUE="true" +KIGFALSE="false" +DEFWIDTH=-1 +DEFCOLOR="#0000ff" +DEFNAME="none" +PROPERTY_INI="Property which" +OBJECT_INI="Object type" +PROPERTY_END="Property" +OBJECT_END="Object" +DICT=(("&","&"), ("<","<"), (">",">"), + ("à","à "), ("è","è"), ("ì","ì"), ("ò","ò"), ("ù","ù"), ("é","é")) + +# +# this is a trick to allow definitions like "p=Point(0,0,HIDDEN)" +# +HIDDEN=KIGFALSE +VISIBLE=KIGTRUE + +##### +# Validation parameters +##### + +def parameter(val, defval): + if val==None: return defval + else: return val + +def validshown(shown): + if shown==KIGTRUE or shown==KIGFALSE: return shown + +def validwidth(width): + if type(width)==TI: return width + +def validpointstyle(ps): + if ps in PS: return ps + +def validname(name): + if type(name)==TS: return name + +def validlinestyle(ls): + if ls in LS: return ls + +def validcolor(color): + if type(color)==TS: return color + +##### +# if as function +##### + +def rif(condition, val1, val2): + """Return val1 if condition is True else return val2.""" + if condition: return val1 + else: return val2 + +##### +# Force some Python variables as kig variables +##### + +def kig_double(val): + tp=type(val) + if tp==TI or tp==TF: return Double(val) + else: return val + +def kig_int(val): + tp=type(val) + if tp==TI: return Int(val) + else: return val + +def kig_string(val): + tp=type(val) + if tp==TS: return String(val) + else: return val + +def kig_point(val): + tp=type(val) + if tp==TT: + x, y = val + return Point(x, y, internal=True) + else: + return val + +def kig_relpoint(obj, displ): + x, y = displ + return RelativePoint(x, y, obj, internal=True) + +##### +# base classes +##### + +##### +# Classe KigDocument +##### + +class KigDocument(object): + """ Classe che produce il documento kig. + + genealogia: + KigDocument <- object + + attributi di classe: + + attributi: + axes + grid + outfilename + outfile + callkig + of + viewkig + hierarchy + internal + width + pointstyle + name + linestyle + shown + color + + metodi: + viewappend + hierarchyappend + setcallkig + setof + str_open + close + noaxes + nogrid + hideobjects + showobjects + setwidth + setpointstyle + setname + setlinestyle + setshown + setcolor + setinternal + """ + + def __init__(self, outfilename, callkig=True, of=False): +# print "KigDocument.__init__()" + self.axes = "1" + self.grid = "1" + self.outfilename=outfilename + self.callkig=callkig + self.of=of + try: + self.outfile = open(outfilename, 'w') + except IOError, value: +# print >> sys.stderr, outfilename, 'unwritable' + print >> sys.stderr, value + sys.exit(2) +# KigOut._kigdocument=self + KigDOP._kd=self + KigView._kd=self + self.viewkig=[] + self.hierarchy=[] +# Defaults values + self.internal=False + self.width=DEFWIDTH + self.pointstyle=PS[0] + self.name=DEFNAME + self.linestyle=LS[0] + self.shown=VISIBLE + self.color=DEFCOLOR + + + def viewappend(self, e): self.viewkig.append(e) + def hierarchyappend(self, e): self.hierarchy.append(e) + def setcallkig(v): self.callkig=v + def setof(v): self.of=v + + def str_open(self): + return """<!DOCTYPE KigDocument> +<KigDocument axes="%s" grid="%s" CompatibilityVersion="0.7.0" Version="0.9.1" > + <CoordinateSystem>Euclidean</CoordinateSystem> + <Hierarchy> +""" % (self.axes, self.grid) + + def close(self): + try: + self.outfile.write(self.str_open()) + self.outfile.writelines(self.hierarchy) + self.outfile.write(" </Hierarchy>\n <View>\n") + for f in self.viewkig: + self.outfile.write(f.str_view()) + self.outfile.write(" </View>\n</KigDocument>\n") + if self.outfile != sys.stdout: + self.outfile.close() + except IOError, value: + print >> sys.stderr, value + sys.exit(2) + try: + if self.callkig: + err = os.system('kig --nofork ' + self.outfilename) + except Exception, value: + print >> sys.stderr, value + if not self.of: + os.system('rm ' + self.outfilename) + + def noaxes(self): self.axes="0" + def nogrid(self): self.grid="0" + def hideobjects(self): self.shown=HIDDEN + def showobjects(self): self.shown=VISIBLE + def setwidth(self, w): self.width=w + def setpointstyle(self, ps): self.pointstyle=ps + def setname(self, n): self.name=n + def setlinestyle(self, ls): self.linestyle=ls + def setshown(self, s): self.shown=s + def setcolor(self, c): self.color=c + def setinternal(self, v): self.internal=v + +##### +# Classe KigDOP +##### + +#class KigDOP(KigOut): +class KigDOP(object): + """Classe da cui deriva ogni elemento che ha un id: Data, Object, Property. + + genealogia: + kigDOP <- object + + attributo di classe: + id-counter + + attributi: + id + type + + metodi: + getid + str_hierarchy + """ + _kd=None + _id_counter=0 + + def __init__(self, type): + KigDOP._id_counter+=1 + self.id=KigDOP._id_counter + self._type=type +# self.getkigdocument().hierarchyappend(self.str_hierarchy()) + KigDOP._kd.hierarchyappend(self.str_hierarchy()) + + def getid(self): return str(self.id) + def str_hierarchy(self): pass + +##### +# Classe KigView +##### + +#class KigView(KigOut): +class KigView(object): + """ Classe con i dati di visualizzazione + + genealogia: + KigView <- object + + attributi di classe: + _kd + + attributi: + shown + width + style + color + name + pointstyle + + metodi: + str_view + show + hide + """ + _kd=None + + def __init__(self, object, shown, name, width, pointstyle, linestyle, color): + self.object=object + self.shown = parameter(shown, KigView._kd.shown) + self.width = parameter(width, KigView._kd.width) + self.pointstyle = parameter(pointstyle, KigView._kd.pointstyle) + self.linestyle = parameter(linestyle, KigView._kd.linestyle) + self.color = parameter(color, KigView._kd.color) + self.name = parameter(name, KigView._kd.name) + KigView._kd.viewappend(self) + + def str_view(self): + """Produce la stringa che viene scritta sotto <View>. + + esempio: + <Draw width="-1" point-style="Round" namecalcer="none" +style="SolidLine" shown=None color="#0000ff" object="3" /> +""" + + return ' <Draw width="%s" point-style="%s" namecalcer="%s"\ + style="%s" shown="%s" color="%s" object="%s" />\n' %\ + (self.width, self.pointstyle, self.name, + self.linestyle, self.shown, self.color, self.object.getid()) + +##### +# Classe Data +##### + +class Data(KigDOP): + """ Classe da cui deriva ogni elemento Data + + genealogia: + Data <- KigDOP <- object + + attributi: + val + + metodi: + str_hierarchy +""" + def __init__(self, type, val): + self.val=val + KigDOP.__init__(self, type) + + def str_hierarchy(self): + """Produce la stringa che viene scritta sotto <Data>. + + esempio: + <Data type="double" id="170" >0.1</Data> +""" + return ' <Data type="%s" id="%s" >%s</Data>\n' % \ + (self._type, self.getid(), self.val) + +##### +# Classe PropObj +##### + +class PropObj(KigDOP): + """ Classe da cui deriva ogni elemento visibile + + genealogia: + PropObj <- KigDOP <- object + + attributi di classe: + + attributi: + prop + objvec + view + + metodi: + str_hierarchy + showname(self, n) + show(self) + hide(self) + setwidth(self, width) + setcolor(self, color) + setlinestyle(self, linestyle) + setpointstyle(self, pointstyle) + setname(self, n) + setshown(self, s) + getwidth(self) + getcolor(self) + getlinestyle(self) + getpointstyle(self) + """ + + def __init__(self, prop, type, objvec, shown, name, internal, + width, pointstyle, linestyle, color): + self.prop=prop + self.objvec=objvec + self.n_lb=None + KigDOP.__init__(self, type) + internal=parameter(internal, KigDOP._kd.internal) + if internal: + self.view = None + else: +# Qui si assume che, se viene dato un nome ad un oggetto, +# si voglia anche visualizzare questo nome + if name: n_id=self.showname(name, shown, width, pointstyle, linestyle, + color) + else: n_id=None + self.view = KigView(self, shown, n_id, width, pointstyle, linestyle, + color) + + def str_hierarchy(self): + """Produce la stringa che viene scritta sotto <Data>. + + esempio: + <Property which="mid-point" id="170" > + <Parent id="..." /> + </Property> + + oppure: + <Object type="ConstrainedPoint" id="14" > + <Parent id="13" /> + <Parent id="10" /> + </Object> +""" + retstring = ' <%s="%s" id="%s" >' %\ + ((self.prop and PROPERTY_INI or OBJECT_INI), + self._type, self.getid()) + for p in self.objvec: + retstring = retstring + '\n <Parent id="%s" />' % p.getid() + retstring = retstring + '\n </%s>\n' % (self.prop and PROPERTY_END or + OBJECT_END) + return retstring + + def showname(self, name, shown, width, pointstyle, linestyle, color): + n=String(name) + self.n_lb=Label(self, (0, 0), n, 0, shown, None, False, + width, pointstyle, linestyle, color) + return n.getid() + + def show(self): + if self.view: self.view.shown=None + def hide(self): + if self.view: self.view.shown=KIGFALSE + def setwidth(self, width): self.view.width=width + def setcolor(self, color): self.view.color=color + def setlinestyle(self, linestyle): + if linestyle in LS: self.view.linestyle=linestyle + def setpointstyle(self, pointstyle): + if pointstyle in PS: self.view.pointstyle=pointstyle + def type(self): return Type(self) + def setname(self, n): + v=self.view + v.name=self.showname(n, v.shown, v.width, v.pointstyle, v.linestyle, + v.color) + def setshown(self, s): self.view.shown=s + +##### +# Classe Property +##### + +class Property(PropObj): + """ Classe da cui deriva ogni elemento Property + + genealogia: + Property <- PropObj <- KigDOP <- object + """ + def __init__(self, type, parent, shown, name, internal, + width, pointstyle, linestyle, color): + PropObj.__init__(self, True, type, (parent,), shown, name, internal, + width, pointstyle, linestyle, color) +# print shown + +##### +# Classe Object +##### + +class Object(PropObj): + """ Classe da cui deriva ogni elemento Oggetto + + genealogia: + Object <- PropObj <- KigDOP <- object + """ + + def __init__(self, type, objvec, shown, name, internal, + width, pointstyle, linestyle, color): + PropObj.__init__(self, False, type, objvec, shown, name, internal, + width, pointstyle, linestyle, color) + +##### +# Data +##### + +data=(\ +("Int", "int", "val"), +("Double", "double", "val"), +("String", "string", "convstr(val)"), +) + +def convstr(s): + for o, n in DICT: + s=s.replace(o, n) + return s + +def databuild(nomeclasse, nomekig, v="val"): + """Create string with a Data class definition.""" + return """class %s(Data): + + def __init__(self, val): + Data.__init__(self, "%s", %s) +""" % (nomeclasse, nomekig, v) + +for d in data: + p1, p2, p3 = d + exec databuild(p1, p2, p3) + +##### +# Objects +##### +"""Da aggiungere: +("ConvexHall", "ConvexHall", "polygon,", "(polygon,),"), +("EllipseByFocusFocusPoint", "EllipseBFFP", "p1, p2, p3,", "(p1, p2, p3,),"), +("HyperbolaByFocusFocusPoint", "HyperbolaBFFP", "p1, p2, p3,", "(p1, p2, p3,),"), +(ConicsBy5Points", "ConicB5P", "p1, p2, p3, p4, p5,", "(p1, p2, p3, p4, p5),"), +("ParabolaBy3Points", "ParabolaBTP", "p1, p2, p3,", "(p1, p2, p3,),"), +("CocCurve", "CocCurve", "line, point,", "(line, point,),"), +""" +objects=(\ +###### Points class +("Point", "FixedPoint", "x, y,", "(kig_double(x), kig_double(y)),"), +("ConstrainedPoint", "ConstrainedPoint", + "t, curve,", "(kig_double(t), curve),"), +("RelativePoint", "RelativePoint", + "x, y, p,", "(kig_double(x), kig_double(y), p),"), +###### segments, rays, lines +("Line", "LineAB", "p1, p2,", "(p1, p2),"), +("Segment", "SegmentAB", "p1, p2,", "(p1, p2),"), +("Ray", "RayAB", "p1, p2,", "(p1, p2),"), +("Orthogonal", "LinePerpend", "line, point,", "(line, point,),"), +("Parallel", "LineParallel", "line, point,", "(line, point,),"), +###### Circles, arcs, ... +("Circle", "CircleBCP", "center, point,", "(center, point,),"), +("CircleByCenterRadius", "CircleBPR", "center, radius,", "(center, radius,),"), +("CircleBy3Points", "CircleBTP", "p1, p2, p3,", "(p1, p2, p3,),"), +("ArcBy3Points", "ArcBTP", "p1, p2, p3,", "(p1, p2, p3,),"), +("ArcByCenterPointAngle", "ArcBCPA", + "center, point, angle,", "(center, point, angle),"), +###### Conics... +("ParabolaByDirectrixFocus", "ParabolaBDP", "line, point,", "(line, point,),"), +("VerticalCubic", "VerticalCubicB4P", "p1, p2, p3, p4,", "(p1, p2, p3, p4),"), +("ConicArc", "ConicArcBTPC", "p1, p2, p3, center,", "(p1, p2, p3, center),"), +##### +# intersections. The only standard object is the intersection +# of two lines, which always gives one single point +##### +("LineLineIntersection", "LineLineIntersection", "l1, l2,", "(l1, l2),"), +##### +# Classe CircleCircleIntersection e ConicLineIntersection +# l'intero "which" puo' assumere i valori 1 o -1 per indicare quale +# delle due intersezioni si desidera ottenere +# si potrebbe mettere un controllo... +##### +("CircleCircleIntersection", "CircleCircleIntersection", + "c1, c2, witch,", "(c1, c2, Int(witch),),"), +("ConicLineIntersection", "ConicLineIntersection", + "conic, line, witch,", "(conic, line, Int(witch),),"), +###### Classe Triangle +("Triangle", "TriangleB3P", "p1, p2, p3,", "(p1, p2, p3),"), +###### Classe Polygon (the only argument is a points vect) +("Polygon", "PolygonBNP", "pvec,", "pvec,"), +###### Classe PolygonBCV +# Poligono regolare dati il centro e un vertice; il terzo argomento +# e' un intero contenente il numero di lati +("PolygonBCV", "PoligonBCV", + "center, vertex, n,", "(center, vertex, Int(n)),"), +##### Classe PolygonVertex (poligono, intero >= 0) +("PolygonVertex", "PolygonVertex", + "polygon, i,", "(polygon, Int(i)),"), +##### Classe PolygonSide (poligono, intero >= 0) +("PolygonSide", "PolygonSide", + "polygon, i,", "(polygon, Int(i)),"), +###### vector, angle,... +("Vector", "Vector", "p1, p2,", "(p1, p2),"), +("Angle", "Angle", "p1, v, p2,", "(p1, v, p2),"), +###### Transformations +("Translate", "Translation", "obj, vector,", "(obj, vector),"), +("CentralSymmetry", "PointReflection", "obj, center,", "(obj, center),"), +("AxialSymmetry", "LineReflection", "obj, center,", "(obj, center),"), +("Rotate", "Rotation", + "obj, center, angle,", "(obj, center, angle),"), +("Scale", "ScalingOverCenter", + "obj, center, segment,", "(obj, center, segment),"), +("Scale2", "ScalingOverCenter2", + "obj, center, s1, s2,", "(obj, center, s1, s2),"), +("InvertPoint", "InvertPoint", + "point, circle,", "(point, circle),"), +("CircularInversion", "CircularInversion", + "objecttoinvert, circle,", "(objecttoinvert, circle),"), +("InvertLine", "InvertLine", + "line, circle,", "(line, circle),"), +("InvertCircle", "InvertCircle", + "circletoinvert, circle,", "(circletoinvert, circle),"), +("InvertArc", "InvertArc", + "arctoinvert, circle,", "(arctoinvert, circle),"), +("InvertSegment", "InvertSegment", + "segment, circle,", "(segment, circle),"), +###### Text, Label, ... +("Text", "Label", + "point, string, boxed=0,", + "(Int(boxed), kig_point(point), kig_string(string)),"), +("Label", "Label", + "obj, displ, string, boxed=0,", + "(Int(boxed),kig_relpoint(obj, displ),kig_string(string)),"), +("VarText", "Label", + "point, string, vars, boxed=0,", + "(Int(boxed), kig_point(point), \ + kig_string(string))+tuple(vars),"), +("VarLabel", "Label", + "obj, displ, string, vars, boxed=0,", + "(Int(boxed), kig_relpoint(obj, displ), \ + kig_string(string))+tuple(vars),"), +###### Python scripting... we need some work here... +("PythonScript", "PythonExecuteType", + "script, argvec,", + '(Object("PythonCompileType", (kig_string(script),), shown,\ + name, internal, width, pointstyle, linestyle,\ + color),)+tuple(argvec),'), +) + +def objectbuild(nameclass, namekig, params, objparams): + """Create string with a Object class definition.""" + return """class %s(Object): + + def __init__(self, %s shown=None, name=None, internal=None, + width=None, pointstyle=None, linestyle=None, color=None): + Object.__init__(self, "%s", %s shown, name, internal, + width, pointstyle, linestyle, color) +""" % (nameclass, params, namekig, objparams) + +for o in objects: + p1, p2, p3, p4 = o + exec objectbuild(p1, p2, p3, p4) + +##### +# Propertys +##### + +property=(\ +("Type", "base-object-type", "o,", "o,"), +("Coordinate", "coordinate", "point,", "point,"), +("XCoord", "coordinate-x", "point,", "point,"), +("YCoord", "coordinate-y", "point,", "point,"), +("MidPoints", "mid-point", "a, b,", "Segment(a, b, internal=True),"), +("MidPoint", "mid-point", "segment,", "segment,"), +("EndPointA", "end-point-A", "segment,", "segment,"), +("EndPointB", "end-point-B", "segment,", "segment,"), +("Length", "length", "segment,", "segment,"), +("Equation", "equation", "segment,", "segment,"), +("Slope", "slope", "segment,", "segment,"), +("NumOfSides", "polygon-number-of-sides", "poly,", "poly,"), +("Perimeter", "polygon-perimeter", "poly,", "poly,"), +("Surface", "polygon-surface", "poly,", "poly,"), +("CenterOfMass", "polygon-center-of-mass", "poly,", "poly,"), +("WindingNumber", "polygon-winding-number", "poly,", "poly,"), +("Radius", "radius", "circle,", "circle,"), +("Center", "center", "circle,", "circle,"), +("Bisector", "angle-bisector", "angle,", "angle,"), +("Support", "support", "object,", "object,"), +) + +def propertybuild(nameclass, namekig, params, objparams): + """Create string with a Property class definition.""" + return """class %s(Property): + + def __init__(self, %s shown=None, name=None, internal=False, + width=None, pointstyle=None, linestyle=None, color=None): + Property.__init__(self, "%s", %s shown, name, internal, + width, pointstyle, linestyle, color) +""" % (nameclass, params, namekig, objparams) + +for p in property: + p1, p2, p3, p4 = p + exec propertybuild(p1, p2, p3, p4) + +##### +# Start of properties definitions as Object's metod +##### +# da sistemare! +points =(Point, ConstrainedPoint, RelativePoint, PolygonVertex) +lines=(Segment, Ray, Vector, InvertLine) +segments=(Segment, Vector, PolygonSide, InvertSegment) +circles =(Circle, CircleBy3Points, CircularInversion, ArcBy3Points, + ArcByCenterPointAngle, InvertCircle) +polygons=(Polygon, PolygonBCV, Triangle) +angles =(Angle,) +supp = circles+lines + +methods=(\ +("coordinate", "coordinate", points), +("coordinate-x", "xcoord", points), +("coordinate-y", "ycoord", points), +("mid-point", "midpoint", segments), +("end-point-A", "endpointA", segments), +("end-point-B", "endpointB", segments), +("length", "length", segments), +("equation", "equation", lines), +("slope", "slope", lines), +("polygon-number-of-sides", "numofsides", polygons), +("polygon-perimeter", "perimeter", polygons), +("polygon-surface", "surface", polygons), +("polygon-center-of-mass", "centerofmass", polygons), +("polygon-winding-number", "windingnumber", polygons), +("center", "center", polygons), +("center", "center", circles), +("angle-bisector", "bisector", angles), +("support", "support", supp), +) + +def methodsbuild(namekig): + """Create string with a method class definition.""" + return """def method(self,shown=None, name=None, internal=False, + width=None, pointstyle=None, linestyle=None, color=None): + return Property("%s", self, shown, name, internal, + width, pointstyle, linestyle, color) +""" % (namekig, ) + +for p in methods: + p1, p2, cl = p + exec methodsbuild(p1) + for c in cl: + setattr(c, p2, method) + +##### +# Usage +##### + +def usage(codexit): + print >> sys.stderr, """ +usage: pykig.py [options...] file ... + +Options: + -h, --help Show this text. + -o <kig_file> + --output <kig_file> output <kig_file> no call Kig + -v, --version output version + -n, --nokig no call Kig + +examples: + $ pykig.py my_file.kpy + $ pykig.py -o output_file.kig my_file.kpy + $ ... +""" + sys.exit(codexit) + +##### +# Main body +##### + +import sys, traceback, os +#from math import * # for user's programs +import math # for user's programs +import getopt +import atexit + +def prog(): + try: + _opts, _args = getopt.getopt(sys.argv[1:], "hvno:",\ + ["help", "version", "nokig", "output="]) + except getopt.GetoptError: + print "GetoptError" + usage(2) + _callKig=True + _of=False + for _opt, _arg in _opts: + if _opt in ("-h", "--help"): + usage(0) + if _opt in ("-v", "--version"): + print "version:", version + sys.exit(0) + if _opt in ("-n", "--nokig"): + _callKig=False + elif _opt in ("-o", "--output"): + _outfilename=_arg + _of=True + _callKig=False # se c'è il file di output, non viene chiamato Kig + if len(_args)==0: + _infilename=raw_input("Nome del file di input: ") + if not _infilename: + print "No Input filename" + usage(2) + elif len(_args)==1: + _infilename=_args[0] + else: + print "No infilename" + usage(2) + try: + _infile = open(_infilename, 'r') + except: + print >> sys.stderr, _infilename, 'unreadable' + sys.exit(2) + if _of: + if _outfilename=="-": + _n, _e = os.path.splitext(_infilename) + _outfilename=_n+'.kig' + else: + _outfilename="/tmp/pykig" + str(os.getpid()) + ".kig" + global kigdocument + kigdocument=KigDocument(_outfilename, _callKig, _of) + kd=kigdocument + try: + execfile(_infilename, globals()) + except: + print >> sys.stderr, 'syntax error in', _infilename + _info = sys.exc_info() # vorrei stampare il traceback... + traceback.print_exc() + sys.exit(3) + kigdocument.close() + + if _infile != sys.stdin: + _infile.close() + +def lib(): + _outfilename="/tmp/pykig" + str(os.getpid()) + ".kig" + global kigdocument + kigdocument=KigDocument(_outfilename) + kd=kigdocument + atexit.register(kigdocument.close) + +if __name__ == "__main__": + prog() +else: + lib() |