#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

def make_pyscript(script)
  script= <<__EOS
#!/usr/lib/freecad
# coding: utf-8

FREECADPATH="/usr/lib/freecad/lib/"
import sys
sys.path.append(FREECADPATH)
import FreeCAD
import Sketcher
import PartDesign
import Mesh

mid_fn,out_fn=sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else ''

__EOS

  script
end

def Calc_size(size,cv,ch)
  if /(\S+)\s+(\S+)/ =~size
    l_size=$1.to_i
    w_size=$2.to_i
  end
  w_max_characters=0
  width=w_size
  while 8<width
    if width-cv<=8
      break
    else
      width-=cv
      w_max_characters+=1
    end
  end
  l_max_characters=0
  length=l_size
  while 8<length
    if length-ch<=8
      break
    else
      length-=ch
      l_max_characters+=1
    end
  end
  return l_size,w_size,w_max_characters,l_max_characters
end
  
def make_ita(ita,bh,l_size,w_size)
  
  ita= <<__EOS
#Gui.activateWorkbench("PartDesignWorkbench")
App.newDocument()
App.setActiveDocument("Unnamed")
App.ActiveDocument=App.getDocument("Unnamed")
#Gui.ActiveDocument=#Gui.getDocument("Unnamed")
App.activeDocument().addObject('Sketcher::SketchObject','Sketch')
App.activeDocument().Sketch.Placement = App.Placement(App.Vector(0.000000,0.000000,0.000000),App.Rotation(0.000000,0.000000,0.000000,1.000000))
#Gui.activeDocument().activeView().setCamera('#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA \\n position 0 0 87 \\n orientation 0 0 1  0 \\n nearDistance -112.88701 \\n farDistance 287.28702 \\n aspectRatio 1 \\n focalDistance 87 \\n height 143.52005 }')
#Gui.activeDocument().setEdit('Sketch')
App.ActiveDocument.Sketch.addGeometry(Part.Line(App.Vector(0,#{l_size},0),App.Vector(#{w_size},#{l_size},0)))
App.ActiveDocument.Sketch.addGeometry(Part.Line(App.Vector(#{w_size},#{l_size},0),App.Vector(#{w_size},0,0)))
App.ActiveDocument.Sketch.addGeometry(Part.Line(App.Vector(#{w_size},0,0),App.Vector(0,0,0)))
App.ActiveDocument.Sketch.addGeometry(Part.Line(App.Vector(0,0,0),App.Vector(0,#{l_size},0)))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Coincident',0,2,1,1))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Coincident',1,2,2,1))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Coincident',2,2,3,1))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Coincident',3,2,0,1))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Horizontal',0))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Horizontal',2))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Vertical',1))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Vertical',3))
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Distance',3,#{l_size}))
App.ActiveDocument.Sketch.setDatum(8,#{l_size}.000000) #ita length
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Distance',0,#{w_size}))
App.ActiveDocument.Sketch.setDatum(9,#{w_size}.000000) #ita width
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Coincident',2,2,-1,1))
App.ActiveDocument.recompute()
#Gui.getDocument('Unnamed').resetEdit()
App.getDocument('Unnamed').recompute()
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Coincident',2,2,-1,1)) 
App.ActiveDocument.recompute()
#Gui.getDocument('Unnamed').resetEdit()
App.getDocument('Unnamed').recompute()
App.activeDocument().addObject("PartDesign::Pad","Pad")
App.activeDocument().Pad.Sketch = App.activeDocument().Sketch
App.ActiveDocument.Pad.Length = #{bh} #ita thickness
App.ActiveDocument.Pad.Reversed = 0
App.ActiveDocument.Pad.Midplane = 0
App.ActiveDocument.Pad.Length2 = 100.000000
App.ActiveDocument.Pad.Type = 0
App.ActiveDocument.Pad.UpToFace = None
App.ActiveDocument.recompute()
#Gui.activeDocument().resetEdit()
App.activeDocument().addObject('Sketcher::SketchObject','Sketch001')
App.activeDocument().Sketch001.Support = (App.ActiveDocument.Pad,["Face6"])
App.activeDocument().recompute()
#Gui.activeDocument().setEdit('Sketch001')
__EOS

ita
end

def make_entyu(ji,ch,tenji,x_line,y_line,count_num1,count_num2,dh,cv,dv)
 
  b=0
  c=0
  hozon_x=x_line
  hozon_y=y_line
  for moji in ji.split("")
    
    while b<=ch[moji].length
      if ch[moji][b]=="1" && count_num2==0
        
        tenji +=<<__EOS
App.ActiveDocument.Sketch001.addGeometry(Part.Circle(App.Vector(#{x_line}, #{y_line},0),App.Vector(0,0,1),  0.75))
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('DistanceX',-2,1,#{count_num2},3,#{x_line}))
App.ActiveDocument.Sketch001.setDatum(#{count_num1},#{x_line})
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('DistanceY',-2,1,#{count_num2},3, #{y_line}))
App.ActiveDocument.Sketch001.setDatum(#{count_num1+=1},#{y_line})
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Radius',#{count_num2},  0.75))
App.ActiveDocument.Sketch001.setDatum(#{count_num1+=1},0.750000)
App.ActiveDocument.recompute()
__EOS
    
        b+=1
        count_num2+=1
      elsif ch[moji][b]=="1" 
    
        if b==0 || b==3
          y_line+=dv 
        end
        y_line-=dv
        tenji+=<<__EOS
App.ActiveDocument.Sketch001.addGeometry(Part.Circle(App.Vector(#{x_line}, #{y_line},0),App.Vector(0,0,1),  0.75))
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('DistanceX',-2,1,#{count_num2},3,#{x_line}))
App.ActiveDocument.Sketch001.setDatum(#{count_num1+=1},#{x_line})
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('DistanceY',-2,1,#{count_num2},3, #{y_line}))
App.ActiveDocument.Sketch001.setDatum(#{count_num1+=1},#{y_line})
App.ActiveDocument.recompute()
App.ActiveDocument.Sketch001.addConstraint(Sketcher.Constraint('Radius',#{count_num2},  0.75))
App.ActiveDocument.Sketch001.setDatum(#{count_num1+=1},0.750000)
App.ActiveDocument.recompute()
__EOS
        b+=1
        count_num2+=1
    
      else
    
        if b==0 || b==3
          y_line+=dv
        end
        y_line-=dv
        b+=1
      end
      if b==3
        x_line+=dh
        c+=1
        y_line=hozon_y
      end
    
    end
    if c==1
      x_line+=cv
      y_line=hozon_y
    else
      x_line+=cv
    end
    b=0
    c=0
  end
  return tenji, count_num1, count_num2 , y_line
end

def end_meisi(meisi,ph)
meisi =<<__EOS
App.ActiveDocument.recompute()
#Gui.getDocument('Unnamed').resetEdit()
App.getDocument('Unnamed').recompute()
App.activeDocument().addObject("PartDesign::Pad","Pad001")
App.activeDocument().Pad001.Sketch = App.activeDocument().Sketch001
App.ActiveDocument.Pad001.Length =#{ph} #tem_length
App.ActiveDocument.Pad001.Reversed = 0
App.ActiveDocument.Pad001.Midplane = 0
App.ActiveDocument.Pad001.Length2 = 100.000000
App.ActiveDocument.Pad001.Type = 0
App.ActiveDocument.Pad001.UpToFace = None
App.ActiveDocument.recompute()
#Gui.activeDocument().resetEdit()
App.getDocument("Unnamed").saveAs("./" + mid_fn)

# Export .stl from .fcstd
# FreeCAD.open(u"./" + im_fn)
__objs__=[]
__objs__.append(FreeCAD.getDocument("Unnamed").getObject("Pad"))

#o = [ FreeCAD.getDocument("Unnamed").findObjects()[0] ]
Mesh.export(__objs__, u"./" + out_fn) 

del __objs__
__EOS
meisi
end

def make_py(ita,fi,la,den,meisi)
  script=String.new
  open("watasu.py", "w:utf-8") do |write|
    write.print(make_pyscript(script))
    write.print("#{ita}")
    write.print("#{fi}")
    write.print("#{la}")
    write.print("#{den}")
    write.print("#{meisi}")
  end
end
