~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to release/scripts/addons_contrib/io_export_dxf/primitive_exporters/curve_exporter.py

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-05-12 20:02:22 UTC
  • mfrom: (14.2.16 sid)
  • Revision ID: package-import@ubuntu.com-20120512200222-lznjs2cxzaq96wua
Tags: 2.63a-1
* New upstream bugfix release
  + debian/patches/: re-worked since source code changed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from .base_exporter import BasePrimitiveDXFExporter
2
 
 
3
 
 
4
 
class CurveDXFExporter(BasePrimitiveDXFExporter):
5
 
    pass
6
 
 
7
 
#-----------------------------------------------------
8
 
def exportCurve(ob, mx, mw, **common):
9
 
    """converts Curve-Object to desired projection and representation(DXF-Entity type)
10
 
    """
11
 
    entities = []
12
 
    block = None
13
 
    curve = ob.getData()
14
 
    #print 'deb: curve=', dir(curve) #---------
15
 
    # TODO: should be: if curve.users>1 and not (PERSPECTIVE or (PROJECTION and HIDDEN_MODE):
16
 
    if INSTANCES and curve.users>1 and not PROJECTION:
17
 
        if curve.name in BLOCKREGISTRY.keys():
18
 
            insert_name = BLOCKREGISTRY[curve.name]
19
 
            # write INSERT to entities
20
 
            entities = exportInsert(ob, mx,insert_name, **common)
21
 
        else:
22
 
            # generate geom_output in ObjectCS
23
 
            imx = mathutils.Matrix().identity()
24
 
            WCS_loc = [0,0,0] # WCS_loc is object location in WorldCoordSystem
25
 
            #print 'deb: WCS_loc=', WCS_loc #---------
26
 
            sizeX = sizeY = sizeZ = 1.0
27
 
            rotX  = rotY  = rotZ = 0.0
28
 
            Thickness,Extrusion,ZRotation,Elevation = None,None,None,None
29
 
            ZRotation,Zrotmatrix,OCS_origin,ECS_origin = None,None,None,None
30
 
            AXaxis = imx[0].copy().resize3D() # = ArbitraryXvector
31
 
            OCS_origin = [0,0,0]
32
 
            if not PROJECTION:
33
 
                #Extrusion, ZRotation, Elevation = getExtrusion(mx)
34
 
                Extrusion, AXaxis = getExtrusion(imx)
35
 
        
36
 
                # no thickness/width for POLYLINEs converted into Screen-C-S
37
 
                #print 'deb: curve.ext1=', curve.ext1 #---------
38
 
                if curve.ext1: Thickness = curve.ext1 * sizeZ
39
 
                if curve.ext2 and sizeX==sizeY:
40
 
                    Width = curve.ext2 * sizeX
41
 
                if "POLYLINE"==curve_as_list[GUI_A['curve_as'].val]: # export as POLYLINE
42
 
                    ZRotation,Zrotmatrix,OCS_origin,ECS_origin = getTargetOrientation(imx,Extrusion,\
43
 
                        AXaxis,WCS_loc,sizeX,sizeY,sizeZ,rotX,rotY,rotZ)
44
 
 
45
 
            entities = writeCurveEntities(curve, imx,
46
 
                Thickness,Extrusion,ZRotation,Elevation,AXaxis,Zrotmatrix,
47
 
                WCS_loc,OCS_origin,ECS_origin,sizeX,sizeY,sizeZ,
48
 
                **common)
49
 
 
50
 
            if entities: # if not empty block
51
 
                # write BLOCK definition and INSERT entity
52
 
                # BLOCKREGISTRY = dictionary 'blender_name':'dxf_name'.append(me.name)
53
 
                BLOCKREGISTRY[curve.name]=validDXFr12name(('CU_'+ curve.name))
54
 
                insert_name = BLOCKREGISTRY[curve.name]
55
 
                block = DXF.Block(insert_name,flag=0,base=(0,0,0),entities=entities)
56
 
                # write INSERT as entity
57
 
                entities = exportInsert(ob, mx, insert_name, **common)
58
 
 
59
 
    else: # no other instances, so go the standard way
60
 
        WCS_loc = ob.loc # WCS_loc is object location in WorldCoordSystem
61
 
        #print 'deb: WCS_loc=', WCS_loc #---------
62
 
        sizeX = ob.SizeX
63
 
        sizeY = ob.SizeY
64
 
        sizeZ = ob.SizeZ
65
 
        rotX  = ob.RotX
66
 
        rotY  = ob.RotY
67
 
        rotZ  = ob.RotZ
68
 
        #print 'deb: sizeX=%s, sizeY=%s' %(sizeX, sizeY) #---------
69
 
    
70
 
        Thickness,Extrusion,ZRotation,Elevation = None,None,None,None
71
 
        ZRotation,Zrotmatrix,OCS_origin,ECS_origin = None,None,None,None
72
 
        AXaxis = mx[0].copy().resize3D() # = ArbitraryXvector
73
 
        OCS_origin = [0,0,0]
74
 
        if not PROJECTION:
75
 
            #Extrusion, ZRotation, Elevation = getExtrusion(mx)
76
 
            Extrusion, AXaxis = getExtrusion(mx)
77
 
    
78
 
            # no thickness/width for POLYLINEs converted into Screen-C-S
79
 
            #print 'deb: curve.ext1=', curve.ext1 #---------
80
 
            if curve.ext1: Thickness = curve.ext1 * sizeZ
81
 
            if curve.ext2 and sizeX==sizeY:
82
 
                Width = curve.ext2 * sizeX
83
 
            if "POLYLINE"==curve_as_list[GUI_A['curve_as'].val]: # export as POLYLINE
84
 
                ZRotation,Zrotmatrix,OCS_origin,ECS_origin = getTargetOrientation(mx,Extrusion,\
85
 
                    AXaxis,WCS_loc,sizeX,sizeY,sizeZ,rotX,rotY,rotZ)
86
 
        entities = writeCurveEntities(curve, mx,
87
 
                Thickness,Extrusion,ZRotation,Elevation,AXaxis,Zrotmatrix,
88
 
                WCS_loc,OCS_origin,ECS_origin,sizeX,sizeY,sizeZ,
89
 
                **common)
90
 
 
91
 
    return entities, block
92
 
 
93
 
 
94
 
#-------------------------------------------------
95
 
def writeCurveEntities(curve, mx,
96
 
        Thickness,Extrusion,ZRotation,Elevation,AXaxis,Zrotmatrix,
97
 
        WCS_loc,OCS_origin,ECS_origin,sizeX,sizeY,sizeZ,
98
 
        **common):
99
 
    """help routine for exportCurve()
100
 
    """
101
 
    entities = []
102
 
    width1,width2 = None, None
103
 
    if 1:
104
 
        for cur in curve:
105
 
            #print 'deb: START cur=', cur #--------------
106
 
            #print 'deb:  dir(curve):',dir(cur)  #---------
107
 
            #print 'deb:  curve.type:',cur.type  #---------
108
 
            points = []
109
 
            flags = []
110
 
            pflag70, pflag75 = 0,0
111
 
 
112
 
            if cur.type==4: # is NURBS
113
 
                #if cur.isNurb():
114
 
                #print 'deb:isNurb --------------'  #---------
115
 
                pflag70 = 4
116
 
                orderU = cur.orderU
117
 
                # curve type:
118
 
                # 0: curvNoFitted
119
 
                # 5: curvQuadraticBspline
120
 
                # 6: curvCubicBspline
121
 
                # 8: curvBezier
122
 
                if orderU<=4: pflag75 = 5
123
 
                elif orderU>=5: pflag75 = 6
124
 
 
125
 
                vflag70 = 16
126
 
                i = -2
127
 
                for point in cur:
128
 
                    #print 'deb:isNurb point=', point #---------
129
 
                    i+=1
130
 
                    if i==orderU-1: i = 0
131
 
                    if i:
132
 
                        flags.append([16, [width1,width2]])
133
 
                    else:
134
 
                        flags.append([8, [width1,width2]])
135
 
                    vec = point[0:3]
136
 
                    #print 'deb: vec=', vec #---------
137
 
                    pkt = mathutils.Vector(vec)
138
 
                    #print 'deb: pkt=', pkt #---------
139
 
                    points.append(pkt)
140
 
                if not cur.isCyclic():
141
 
                    points = points[1:-1]
142
 
                    flags = flags[1:-1]
143
 
            elif cur.type==1: # is Bezier
144
 
                #print 'deb:isBezier --------------'  #---------
145
 
                pflag75 = 8
146
 
                vflag70 = 1
147
 
                for point in cur:
148
 
                    #print 'deb:isBezier point=', point #---------
149
 
                    #print 'deb:isBezier point=', point.getTriple() #---------
150
 
                    ptan1,pfit,ptan2 = point.getTriple()
151
 
                    #print 'deb: point=', pt #---------
152
 
                    ptan1 = mathutils.Vector(ptan1)
153
 
                    pfit = mathutils.Vector(pfit)
154
 
                    ptan2 = mathutils.Vector(ptan2)
155
 
                    #print 'deb: pkt=', pkt #---------
156
 
                    points.append(ptan1)
157
 
                    flags.append([2, [width1,width2]])
158
 
                    points.append(pfit)
159
 
                    flags.append([1, [width1,width2]])
160
 
                    points.append(ptan2)
161
 
                    flags.append([2, [width1,width2]])
162
 
                if not cur.isCyclic():
163
 
                    points = points[1:-1]
164
 
                    flags = flags[1:-1]
165
 
            elif cur.type==0: # is Polygon
166
 
                #print 'deb:isPolygon --------------'  #---------
167
 
                #pflag70 = 4
168
 
                pflag75 = 0
169
 
                for point in cur:
170
 
                    #print 'deb:isPoly point=', point #---------
171
 
                    vec = point[0:3]
172
 
                    #print 'deb: vec=', vec #---------
173
 
                    pkt = mathutils.Vector(vec)
174
 
                    #print 'deb: pkt=', pkt #---------
175
 
                    points.append(pkt)
176
 
                    flags.append([None, [width1,width2]])
177
 
    
178
 
            #print 'deb: points', points #--------------
179
 
            if len(points)>1:
180
 
                c = curve_as_list[GUI_A['curve_as'].val]
181
 
 
182
 
                if c=="POLYLINE": # export Curve as POLYLINE
183
 
                    if not PROJECTION:
184
 
                        # recalculate points(2d=X,Y) into Entity-Coords-System
185
 
                        for p in points: # list of vectors
186
 
                            p[0] *= sizeX
187
 
                            p[1] *= sizeY
188
 
                            p2 = p * Zrotmatrix
189
 
                            p2[0] += ECS_origin[0]
190
 
                            p2[1] += ECS_origin[1]
191
 
                            p[0],p[1] = p2[0],p2[1]
192
 
                    else:
193
 
                        points = projected_co(points, mx)
194
 
                    #print 'deb: points', points #--------------
195
 
    
196
 
                    if cur.isCyclic(): closed = 1
197
 
                    else: closed = 0
198
 
                    points = toGlobalOrigin(points)
199
 
                    points_temp = []
200
 
                    for p,f in zip(points,flags):
201
 
                        points_temp.append([p,f[0],f[1]])    
202
 
                    points = points_temp
203
 
                    #print 'deb: points', points #--------------
204
 
 
205
 
                    if DEBUG: curve_drawBlender(points,OCS_origin,closed) #deb: draw to scene
206
 
 
207
 
                    common['extrusion']= Extrusion
208
 
                    ##common['rotation']= ZRotation
209
 
                    ##common['elevation']= Elevation
210
 
                    common['thickness']= Thickness
211
 
                    #print 'deb: common=', common #------------------
212
 
    
213
 
                    flag70, flag75 = pflag70+closed, pflag75
214
 
                    if 0: #DEBUG
215
 
                        p=AXaxis[:3]
216
 
                        entities.append(DXF.Line([[0,0,0], p],**common))
217
 
                        p=ECS_origin[:3]
218
 
                        entities.append(DXF.Line([[0,0,0], p],**common))
219
 
                        common['color']= 5
220
 
                        p=OCS_origin[:3]
221
 
                        entities.append(DXF.Line([[0,0,0], p],**common))
222
 
                        #OCS_origin=[0,0,0] #only debug----------------
223
 
                        dxfPLINE = DXF.PolyLine(points,OCS_origin, flag70=flag70, flag75=flag70, width=0.0,**common)
224
 
                        entities.append(dxfPLINE)
225
 
    
226
 
                    dxfPLINE = DXF.PolyLine(points,OCS_origin, flag70=flag70, flag75=flag70, width=0.0,**common)
227
 
                    entities.append(dxfPLINE)
228
 
                    if Thickness:
229
 
                        common['thickness']= -Thickness
230
 
                        dxfPLINE = DXF.PolyLine(points,OCS_origin, flag70=flag70, flag75=flag70, width=0.0,**common)
231
 
                        entities.append(dxfPLINE)
232
 
    
233
 
                elif c=="LINEs": # export Curve as multiple LINEs
234
 
                    points = projected_co(points, mx)
235
 
                    if cur.isCyclic(): points.append(points[0])
236
 
                    #print 'deb: points', points #--------------
237
 
                    points = toGlobalOrigin(points)
238
 
    
239
 
                    if DEBUG: curve_drawBlender(points,WCS_loc,closed) #deb: draw to scene
240
 
                    common['extrusion']= Extrusion
241
 
                    common['elevation']= Elevation
242
 
                    common['thickness']= Thickness
243
 
                    #print 'deb: common=', common #------------------
244
 
                    for i in range(len(points)-1):
245
 
                        linepoints = [points[i], points[i+1]]
246
 
                        dxfLINE = DXF.Line(linepoints,**common)
247
 
                        entities.append(dxfLINE)
248
 
                    if Thickness:
249
 
                        common['thickness']= -Thickness
250
 
                        for i in range(len(points)-1):
251
 
                            linepoints = [points[i], points[i+1]]
252
 
                            dxfLINE = DXF.Line(linepoints,**common)
253
 
                            entities.append(dxfLINE)
254
 
    
255
 
                elif c=="POINTs": # export Curve as multiple POINTs
256
 
                    points = projected_co(points, mx)
257
 
                    for p in points:
258
 
                        dxfPOINT = DXF.Point(points=[p],**common)
259
 
                        entities.append(dxfPOINT)
260
 
    return entities