ホーム
ニュース e-mail

python-1


<python child process on FreeCAD>
− iomodule.py −


#import FreeCAD
import Part
from FreeCAD import Base
from math import sqrt,pow,cos,pi,atan2
import re
import platform



def cutleading0(string):
	cobj=re.search(r'[^0]',string)
	if cobj!=None:
		ctt=cobj.start()
		return string[ctt:]
	else:
		return "0"

def xyrectf(lb,rt,rfilletlb,rfilletlt,rfilletrt,rfilletrb):
	lb6x=lb[0]+rfilletlb
	lb6y=lb[1]
	lb9x=lb[0]
	lb9y=lb[1]+rfilletlb
	lt9x=lb[0]
	lt9y=rt[1]-rfilletlt
	lt0x=lb[0]+rfilletlt
	lt0y=rt[1]
	rt0x=rt[0]-rfilletrt
	rt0y=rt[1]
	rt3x=rt[0]
	rt3y=rt[1]-rfilletrt
	rb3x=rt[0]
	rb3y=lb[1]+rfilletrb
	rb6x=rt[0]-rfilletrb
	rb6y=lb[1]
	linel=Part.Line(Base.Vector(lb9x,lb9y,0),Base.Vector(lt9x,lt9y,0))
	linet=Part.Line(Base.Vector(lt0x,lt0y,0),Base.Vector(rt0x,rt0y,0))
	liner=Part.Line(Base.Vector(rt3x,rt3y,0),Base.Vector(rb3x,rb3y,0))
	lineb=Part.Line(Base.Vector(rb6x,rb6y,0),Base.Vector(lb6x,lb6y,0))
	c45d=1-cos(pi/4)
	lbf45=rfilletlb*c45d
	ltf45=rfilletlt*c45d
	rtf45=rfilletrt*c45d
	rbf45=rfilletrb*c45d
	arclb=Part.Arc(Base.Vector(lb6x,lb6y,0),Base.Vector(lb[0]+lbf45,lb[1]+lbf45,0),Base.Vector(lb9x,lb9y,0))
	arclt=Part.Arc(Base.Vector(lt9x,lt9y,0),Base.Vector(lb[0]+ltf45,rt[1]-ltf45,0),Base.Vector(lt0x,lt0y,0))
	arcrt=Part.Arc(Base.Vector(rt0x,rt0y,0),Base.Vector(rt[0]-rtf45,rt[1]-rtf45,0),Base.Vector(rt3x,rt3y,0))
	arcrb=Part.Arc(Base.Vector(rb3x,rb3y,0),Base.Vector(rt[0]-rbf45,lb[1]+rbf45,0),Base.Vector(rb6x,rb6y,0))

	shaperectf=Part.Shape([linel,linet,liner,lineb,arclb,arclt,arcrt,arcrb])
	shapewire=Part.Wire(shaperectf.Edges)
	return shapewire

def soval(doc,xystart,xyend,linewidth,zthick):
	zposi=xystart[2]
	rtmsq=sqrt(pow((xyend[0]-xystart[0]),2)+pow((xyend[1]-xystart[1]),2))
	cosine=(xyend[0]-xystart[0])/rtmsq
	sine=(xyend[1]-xystart[1])/rtmsq
	xd=linewidth*sine/2
	yd=linewidth*cosine/2
	x1=xd
	y1=yd
	x3=xd
	y3=yd
	x2=linewidth*cosine/2
	y2=linewidth*sine/2
	xs1=xystart[0]+xd
	ys1=xystart[1]-yd
	xs3=xystart[0]-xd
	ys3=xystart[1]+yd
	xs2=xystart[0]-x2
	ys2=xystart[1]-y2
	xe1=xyend[0]+xd
	ye1=xyend[1]-yd
	xe3=xyend[0]-xd
	ye3=xyend[1]+yd
	xe2=xyend[0]+x2
	ye2=xyend[1]+y2
	line1=Part.Line(Base.Vector(xs1,ys1,zposi),Base.Vector(xe1,ye1,zposi))
	line2=Part.Line(Base.Vector(xs3,ys3,zposi),Base.Vector(xe3,ye3,zposi))
	arc1=Part.Arc(Base.Vector(xs1,ys1,zposi),Base.Vector(xs2,ys2,zposi),Base.Vector(xs3,ys3,zposi))
	arc2=Part.Arc(Base.Vector(xe1,ye1,zposi),Base.Vector(xe2,ye2,zposi),Base.Vector(xe3,ye3,zposi))
	shapeoval=Part.Shape([line1,line2,arc1,arc2])
	shapewire=Part.Wire(shapeoval.Edges)
	shapeface=Part.Face(shapewire)
	shapesolid=shapeface.extrude(Base.Vector(0,0,zthick))
	#Part.show(shapesolid)
	soval=doc.addObject("Part::Feature","")
	soval.Shape=shapesolid
	return soval
	
def sbox(doc,xystart,boxlength,boxwidth,boxheight):
	zposi=xystart[2]
	xl=xystart[0]-boxlength/2
	xr=xystart[0]+boxlength/2
	yb=xystart[1]-boxwidth/2
	yt=xystart[1]+boxwidth/2
	line1=Part.Line(Base.Vector(xl,yb,zposi),Base.Vector(xr,yb,zposi))
	line2=Part.Line(Base.Vector(xl,yt,zposi),Base.Vector(xr,yt,zposi))
	line3=Part.Line(Base.Vector(xl,yb,zposi),Base.Vector(xl,yt,zposi))
	line4=Part.Line(Base.Vector(xr,yb,zposi),Base.Vector(xr,yt,zposi))
	shapebox=Part.Shape([line1,line2,line3,line4])
	shapewire=Part.Wire(shapebox.Edges)
	shapeface=Part.Face(shapewire)
	shapesolid=shapeface.extrude(Base.Vector(0,0,boxheight))
	#Part.show(shapesolid)
	sbox=doc.addObject("Part::Feature","")
	sbox.Shape=shapesolid
	return sbox

def scyl(doc,xystart,cdiam,cylheight):
	zposi=xystart[2]
	#circle1=Part.Circle(xystart,Base.Vector(0,0,1),cdiam/2)
	circle1=Part.makeCircle(cdiam/2,xystart,Base.Vector(0,0,1))
	shapewire=Part.Wire(circle1)
	shapeface=Part.Face(shapewire)
	shapesolid=shapeface.extrude(Base.Vector(0,0,cylheight))
	#Part.show(shapesolid)
	scyl=doc.addObject("Part::Feature","")
	scyl.Shape=shapesolid
	return scyl

def tboxline(xystart,xyend,linewidth,zthick):
	zposi=xystart[2]
	delx=xyend[0]-xystart[0]
	dely=xyend[1]-xystart[1]
	rtmsq=sqrt(pow((delx),2)+pow((dely),2))
	cosine=(delx)/rtmsq
	sine=(dely)/rtmsq
	xd=linewidth*sine/2
	yd=linewidth*cosine/2
	degreeangle=atan2(dely,delx)*180/pi
	shapeBox=Part.makeBox(rtmsq,linewidth,zthick,xystart,Base.Vector(0,0,1)) 
	shapeBox.rotate(xystart,Base.Vector(0,0,1),degreeangle)
	shapeBox.translate(Base.Vector(xd,-yd,0))
	Part.show(shapeBox)
	
def tbox(xystart,rectwidth,rectheight,zthick):
	zposi=xystart[2]
	xd=rectwidth/2
	yd=rectheight/2
	shapeBox=Part.makeBox(rectwidth,rectheight,zthick,xystart,Base.Vector(0,0,1)) 
	shapeBox.translate(Base.Vector(-xd,-yd,0))
	Part.show(shapeBox)
	
def tcyl(xyposi,cdiam,zthick):
	zposi=xyposi[2]
	shapeCylinder=Part.makeCylinder(cdiam/2,zthick,xyposi,Base.Vector(0,0,1)) 
	Part.show(shapeCylinder)

def dboxline(doc,xystart,xyend,boxwidth,boxheight):
	zposi=xystart[2]
	delx=xyend[0]-xystart[0]
	dely=xyend[1]-xystart[1]
	rtmsq=sqrt(pow((delx),2)+pow((dely),2))
	cosine=(delx)/rtmsq
	sine=(dely)/rtmsq
	xd=boxwidth*sine/2
	yd=boxwidth*cosine/2
	degreeangle=atan2(dely,delx)*180/pi
	position=Base.Vector(xystart[0]+xd,xystart[1]-yd,xystart[2])
	db1=doc.addObject("Part::Box","")
	db1.Length=rtmsq
	db1.Width=boxwidth
	db1.Height=boxheight
	db1.Placement=App.Placement(position,App.Rotation(App.Vector(0,0,1),degreeangle))
	return db1
	
def dbox(doc,xystart,boxlength,boxwidth,boxheight):
	position=Base.Vector(xystart[0]-boxlength/2,xystart[1]-boxwidth/2,xystart[2])
	db1=doc1.addObject("Part::Box","")
	db1.Length=boxlength
	db1.Width=boxwidth
	db1.Height=boxheight
	db1.Placement=App.Placement(position,App.Rotation(App.Vector(0,0,1),0))
	return db1
	
def dcyl(doc,position,cdiam,zthick):
	dc1=doc.addObject("Part::Cylinder","")
	dc1.Radius=cdiam/2
	dc1.Height=zthick
	dc1.Placement=App.Placement(position,App.Rotation(App.Vector(0,0,1),0))
	return dc1 

def dolayerfile(doc,filename,name,calorigin,zposi,zthick,callimleft,callimright,callimbottom,callimtop):
	ld = open(filename)
	lines = ld.readlines()
	ld.close()

	dcode=[["Z",-1,"Z",-1,"Z",-1,"Z",-1,"Z",-1] for j in range(1000)]
	stt0=3
	codenumber=0
	for nm in lines:
		if nm.find("%AD")==0:
			OneLine=nm[:-1]
			sobj=re.search(r'[0-9]',OneLine[stt0:])
			if sobj!=None:
				stt1=sobj.start()+stt0
				sobj=re.search(r'[A-Za-z]',OneLine[stt1:])
				if sobj!=None:
					stt2=sobj.start()+stt1
					gotnum=int(cutleading0(OneLine[stt1:stt2]))
					if gotnum<0 or 999<gotnum: 
						codenumber=999
					else:
						codenumber=gotnum
					sobj=re.search(r',',OneLine[stt2:])
					if sobj!=None:
						stt3=sobj.start()+stt2
						gotstr=OneLine[stt2:stt3]
						dcode[codenumber][0]=gotstr
						sobj=re.search(r'[0-9.]',OneLine[stt3:])
						if sobj!=None:
							stt4=sobj.start()+stt3
							sobj=re.search(r'[^0-9.]',OneLine[stt4:])
							if sobj!=None:
								stt5=sobj.start()+stt4
								gotnum=int(float(cutleading0(OneLine[stt4:stt5]))*10000)
								dcode[codenumber][1]=gotnum
								sobj=re.search(r'[0-9.]',OneLine[stt5:])
								if sobj!=None:
									stt6=sobj.start()+stt5
									gotstr=OneLine[stt5:stt6]
									dcode[codenumber][2]=gotstr
									sobj=re.search(r'[^0-9.]',OneLine[stt6:])
									if sobj!=None:
										stt7=sobj.start()+stt6
										gotnum=int(float(cutleading0(OneLine[stt6:stt7]))*10000)
										dcode[codenumber][3]=gotnum
										sobj=re.search(r'[0-9.]',OneLine[stt7:])
										if sobj!=None:
											stt8=sobj.start()+stt7
											gotstr=OneLine[stt7:stt8]
											dcode[codenumber][4]=gotstr
											sobj=re.search(r'[^0-9.]',OneLine[stt8:])
											if sobj!=None:
												stt9=sobj.start()+stt8
												gotnum=int(float(cutleading0(OneLine[stt8:stt9]))*10000)
												dcode[codenumber][5]=gotnum
												sobj=re.search(r'[0-9.]',OneLine[stt9:])
												if sobj!=None:
													stt10=sobj.start()+stt9
													gotstr=OneLine[stt9:stt10]
													dcode[codenumber][6]=gotstr
													sobj=re.search(r'[^0-9.]',OneLine[stt10:])
													if sobj!=None:
														stt11=sobj.start()+stt10
														gotnum=int(float(cutleading0(OneLine[stt10:stt11]))*10000)
														dcode[codenumber][7]=gotnum
														sobj=re.search(r'[0-9.]',OneLine[stt11:])
														if sobj!=None:
															stt12=sobj.start()+stt11
															gotstr=OneLine[stt11:stt12]
															dcode[codenumber][8]=gotstr
															sobj=re.search(r'[^0-9.]',OneLine[stt12:])
															if sobj!=None:
																stt13=sobj.start()+stt12
																gotnum=int(float(cutleading0(OneLine[stt12:stt13]))*10000)
																dcode[codenumber][9]=gotnum
																sobj=re.search(r'[0-9.]',OneLine[stt13:])

	vs=Base.Vector(0,0,0)
	ve=Base.Vector(0,0,0)
	pattern="Z"
	dclist=[]
	for nm in lines:
		if nm.find("D")==0:
			OneLine=nm[:-1]
			sta=1
			stb=OneLine[sta:].find('*')+sta
			codenumber=int(cutleading0(OneLine[sta:stb]))
			pattern=dcode[codenumber][0]
		elif nm.find("X")==0:
			OneLine=nm[:-1]
			sta=1
			sobj=re.search(r'[0-9]',OneLine[sta:])
			stb=sobj.start()+sta
			sobj=re.search(r'[^0-9]',OneLine[stb:])
			stc=sobj.start()+stb
			sobj=re.search(r'[0-9]',OneLine[stc:])
			std=sobj.start()+stc
			sobj=re.search(r'[^0-9]',OneLine[std:])
			ste=sobj.start()+std
			stf=OneLine[ste:].find('*')+ste
			xstr=cutleading0(OneLine[stb:stc])
			ystr=cutleading0(OneLine[std:ste])
			dfunc=OneLine[ste:stf]
			if dfunc=="D02":
				vs=Base.Vector(int(xstr),int(ystr),zposi)
			elif dfunc=="D03":
				vs=Base.Vector(int(xstr),int(ystr),zposi)
				if callimleft<vs[0] and vs[0]<callimright and callimbottom<vs[1] and vs[1]<callimtop:
					if pattern=="C":
						cdiam=dcode[codenumber][1]
						primobj=scyl(doc,vs,cdiam,zthick)
						#primobj=dcyl(doc,vs,cdiam,zthick)
						dclist.append(primobj)
					elif pattern=="R":
						boxlength=dcode[codenumber][1]
						boxwidth=dcode[codenumber][3]
						primobj=sbox(doc,vs,boxlength,boxwidth,zthick)
						#primobj=dbox(doc,vs,boxlength,boxwidth,zthick)
						dclist.append(primobj)
			elif dfunc=="D01":
				ve=Base.Vector(int(xstr),int(ystr),zposi)
				if vs!=ve:
					if (callimleft<vs[0] and vs[0]<callimright and callimbottom<vs[1] and vs[1]<callimtop) or (callimleft<ve[0] and ve[0]<callimright and callimbottom<ve[1] and ve[1]<callimtop) or (callimleft<vs[0] and vs[0]<callimright and callimleft<ve[0] and ve[0]<callimright and((vs[1]<callimbottom and callimtop<ve[1])or(ve[1]<callimbottom and callimtop<vs[1]))) or (callimbottom<vs[1] and vs[1]<callimtop and callimbottom<ve[1] and ve[1]<callimtop and ((vs[0]<callimleft and callimright<ve[0])or(ve[0]<callimleft and callimright<vs[0]))):
						linewidth=dcode[codenumber][1]
						cdiam=linewidth
						primobj=soval(doc,vs,ve,linewidth,zthick)
						dclist.append(primobj)
						#primobj1=dboxline(doc,vs,ve,linewidth,zthick)
						#primobj2=dcyl(doc,ve,cdiam,zthick)
						#dclist.append(primobj1)
						#dclist.append(primobj2)
					vs=ve
			else:
				FreeCAD.Console.PrintMessage("Not a Draw Function.\n")
	dcresult=doc.addObject("Part::Compound",name)
	dcresult.Links=dclist

def dodrillfile(doc,filename,name,calorigin,zposi,zthick,callimleft,callimright,callimbottom,callimtop):
	ld = open(filename)
	lines = ld.readlines()
	ld.close()

	tcode=[["Z",-1] for j in range(100)]
	codenumber=0
	configstart=-1
	configend=-1
	i=0
	for nm in lines:
		if nm.find("%")==0:
			if configstart==-1:
				configstart=i
			elif configend==-1:
				configend=i
		else: 
			if configstart>=0 and configend==-1:
				OneLine=nm[:-1] 
				if OneLine[0:1]=="T":
					sobj=re.search(r'[0-9.]',OneLine[0:])
					if sobj!=None:
						stt1=sobj.start()
						if stt1==1:
							sobj=re.search(r'[^0-9.]',OneLine[stt1:])
							if sobj!=None:
								stt2=sobj.start()+stt1
								gotnum=int(cutleading0(OneLine[stt1:stt2]))
								if gotnum<0 or 999<gotnum: 
									codenumber=999
								else:
									codenumber=gotnum
								sobj=re.search(r'[0-9.]',OneLine[stt2:])
								if sobj!=None:
									stt3=sobj.start()+stt2
									gotstr=OneLine[stt2:stt3]
									tcode[codenumber][0]=gotstr
									sobj=re.search(r'[^0-9.]',OneLine[stt3:])
									if sobj!=None:
										stt4=sobj.start()+stt3
										gotstr=OneLine[stt3:stt4]
									else:
										gotstr=OneLine[stt3:]
									gotnum=int(float(gotstr)*10000)
									tcode[codenumber][1]=gotnum
		i+=1

	vs=Base.Vector(0,0,0)
	ve=Base.Vector(0,0,0)
	pattern="Z"
	dclist=[]
	for nm in lines[configend+1:]:
		if nm.find("T")==0:
			OneLine=nm[:-1]
			sta=1
			codenumber=int(cutleading0(OneLine[sta:]))
			cdiam=tcode[codenumber][1]
		elif nm.find("X")==0:
			OneLine=nm[:-1]
			sta=1
			sobj=re.search(r'[0-9]',OneLine[sta:])
			stb=sobj.start()+sta
			sobj=re.search(r'[^0-9]',OneLine[stb:])
			stc=sobj.start()+stb
			sobj=re.search(r'[0-9]',OneLine[stc:])
			std=sobj.start()+stc
			xstr=cutleading0(OneLine[stb:stc])
			ystr=cutleading0(OneLine[std:])
			vs=Base.Vector(int(xstr),int(ystr),zposi)
			if callimleft<vs[0] and vs[0]<callimright and callimbottom<vs[1] and vs[1]<callimtop:
				primobj=dcyl(doc,vs,cdiam,zthick)
				dclist.append(primobj)
	dcresult=doc.addObject("Part::Compound",name)
	dcresult.Links=dclist















guiMode=(FreeCAD.ConfigGet("RunMode")=="Gui")
system=platform.system()
FreeCAD.Console.PrintMessage("platform system is "+system+"\n")
if system=="Linux":
	filepath="/home/jiji/MyFreeCAD/"
	gerberpath="/home/jiji/MyFreeCAD/gerber/"
else:
	filepath="C:\\Users\\jiji\\MyFreeCAD\\"
	gerberpath="C:\\Users\\jiji\\MyFreeCAD\\gerber\\"
	
#doc=FreeCAD.newDocument("tag")
doc1=App.newDocument("tag1")
#doc2=App.newDocument("tag2")
#a=App.ActiveDocument=App.getDocument("tag1")
#App.closeDocument("tag2")



arealeft=8000
arearright=50000
areabottom=1000
areatop=30000
areafillet=500

zthickstc=100
zposistc=2000
zthickcmp=200
zposicmp=1600
zthickdrl=800
zposidrl=800
zthicksol=200
zposisol=600
zthicksts=100
zposists=300



div10left=7.5
div10right=8.5
div10bottom=3.3
div10top=4.3

callimleft=arealeft+(div10left-1)*(arearright-arealeft)/10
callimright=arealeft+(div10right)*(arearright-arealeft)/10
callimbottom=areabottom+(div10bottom-1)*(areatop-areabottom)/10
callimtop=areabottom+(div10top)*(areatop-areabottom)/10
calorigin=Base.Vector(callimleft,callimbottom,0)

shapewireprg=xyrectf(Base.Vector(callimleft,callimbottom,0),Base.Vector(callimright,callimtop,0),areafillet,areafillet,areafillet,areafillet)
shapefaceprg=Part.Face(shapewireprg)
shapeprg=shapefaceprg.extrude(Base.Vector(0,0,-50))
d1prg=doc1.addObject("Part::Feature","prg")
d1prg.Shape=shapeprg

dolayerfile(doc1,gerberpath+"qein1.sol","sol",calorigin,zposisol,zthicksol,callimleft,callimright,callimbottom,callimtop)

dolayerfile(doc1,gerberpath+"qein1.cmp","cmp",calorigin,zposicmp,zthickcmp,callimleft,callimright,callimbottom,callimtop)

dolayerfile(doc1,gerberpath+"qein1.stc","stc",calorigin,zposistc,zthickstc,callimleft,callimright,callimbottom,callimtop)

dolayerfile(doc1,gerberpath+"qein1.sts","sts",calorigin,zposists,zthicksts,callimleft,callimright,callimbottom,callimtop)

dodrillfile(doc1,gerberpath+"qein1.drd","drh",calorigin,zposidrl,zthickdrl,callimleft,callimright,callimbottom,callimtop)



#doc1.recompute()
#doc1.saveAs('/home/jiji/MyFreeCAD/pcb3.fcstd')

doc1.recompute()
exportlist=[doc1.prg,doc1.stc,doc1.cmp,doc1.drh,doc1.sol,doc1.sts] 

if (guiMode):
	import importOBJ
	importOBJ.export(exportlist,filepath+"pcb3.obj")

if (guiMode):
	import ImportGui
	ImportGui.export(exportlist,filepath+"pcb3.stp")
else:
	import Import
	Import.export(exportlist,filepath+"pcb3.stp")

#d1allcompound=doc1.addObject("Part::Compound","")
#d1allcompound.Links=exportlist
#doc1.recompute() 
#d1allcompound.Shape.exportStep(filepath+"pcb3.stp")



if (guiMode):
	readfilename=filepath+"pcb3.obj"
	writefilename=filepath+"pcb3mtl.obj"
	try:
		rf=open(readfilename,"r")
	except:
		FreeCAD.Console.PrintMessage("exception while open(r).\n") 
	else:
		rlines=rf.readlines()
		rf.close()
		try:
			wf=open(writefilename,"w")
		except:
			FreeCAD.Console.PrintMessage("exception while open(w).\n") 
		else:
			try:
				i=0
				for oneline in rlines:
					if oneline.find("#")==0:
						wf.write(oneline)
						i+=1
					else:
						wf.write("# BieeWave: Material Informations Inserted\n")
						wf.write("mtllib pcb3_6layers.mtl\n")
						break
				for oneline in rlines[i:]:
					wf.write(oneline)
					if oneline.find("o ")==0:
						if oneline[2:5]=="prg":
							wf.write("usemtl prg\n")
						elif oneline[2:5]=="sts":
							wf.write("usemtl sts\n")
						elif oneline[2:5]=="sol":
							wf.write("usemtl sol\n")
						elif oneline[2:5]=="drh":
							wf.write("usemtl drh\n")
						elif oneline[2:5]=="cmp":
							wf.write("usemtl cmp\n")
						elif oneline[2:5]=="stc":
							wf.write("usemtl stc\n")
			except:
				FreeCAD.Console.PrintMessage("exception while obj(mtl) file creating.\n")
			else:
				FreeCAD.Console.PrintMessage("obj(mtl) file created.\n")
			wf.close()				

FreeCAD.Console.PrintMessage("Python End.\n")

			



ビーウエイブ・テクノロジー
bieewave.com