summaryrefslogtreecommitdiff
path: root/GenSCAD.hs
blob: 90bcd741d3e02e80c0623eb86409bcb0894973af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module GenSCAD
( placecube
) where

import Linear.V3
import Control.Lens
import Data.List
import Text.Printf
import System.IO.Temp
import System.Process

_V32l :: R3 c1 => c1 c -> [c]
_V32l c = [c ^. _x, c ^. _y, c ^. _z]

_V32s :: (R3 c1, Show a) => c1 a -> [Char]
_V32s c = intercalate "," (map show (_V32l c))

-- placecube: OpenSCAD translate cube described by d to place designated by p
-- placecube (V3 1 2 3, V3 10 20 30) ->
--   translate([1, 2, 3]) { cube([10, 20, 30]); }
placecube obj  = "translate([ " ++ _V32s(fst obj) ++ " ]) { cube([ " ++ _V32s(snd obj) ++ " ]); }"
--placecube (p,d)  = printf "translate([%d, %d, %d]) { cube([%d, %d, %d]); }" (_V32l p ++ _V32l d)


-- take a list of tuples containing positions and dimensions for cubes and
-- generate corresponding code for rendering in OpenSCAD
-- genscad [(V3 0 0 0, V3 10 10 10), (V3 20 20 20, V3 10 10 10)] ->
--     placecube (V3 0 0 0, V3 10 10 10) -> translate ([...]) { cube([...]);}
--     placecube (V3 20 20 20, V3 10 10 10) -> '

td = [(V3 0 1 2, V3 10 20 30), (V3 50 51 52, V3 11 22 33)]

--genscad :: [(V3 p, V3 d)] -> [Char]
--genscad objs = concat $ map (++ "\n") $ map placecube objs
genscad objs = concat $ map (++ "\n") $ map (placecube) objs
printscad objs = putStrLn $ genscad objs
--writefilescad objs = withSystemTempFile "genscad" -> fp gsf -> doprintscad objs
writetempscad objs = writeSystemTempFile "genscad" (genscad objs)
opentempscad objs = runCommand ("openscad " ++ "foo.scad")
--opentempscad objs = 

-- savegenscad code file -> save generated code file
-- opensavegenscad code file -> generate scad, save to file, then open in OpenSCAD

-- readEditor :: IO String
-- readEditor = withSystemTempFile "read-editor" readEditor'

-- -- | Opens a file, fills it some content and returns it's contents after it's saved.
-- readEditorWith :: String -> IO String
-- readEditorWith contents = withSystemTempFile "read-editor" $ \fp temph -> do
--     hPutStr temph contents
--     hFlush temph
--     readEditor' fp temph

-- readEditor' :: FilePath -> Handle -> IO String
-- readEditor' fp temph = do
--     openEditor fp
--     hClose temph
-- readFile fp


  
--   compileRunPrint :: FilePath -> Ident -> IO String
-- compileRunPrint agdap var =
--   withSystemTempFile "module.mlf" $
--     \mlfp mlfh -> do
--       callProcess "stack" ["exec", "agda-ocaml", "--", "-v0", "--mlf", agdap
--                           , "-o", mlfp, "--print-var", var]
--       runModFile' mlfp mlfh

-- compileRun :: FilePath -> IO String
-- compileRun agdap =
--   withSystemTempFile "module.mlf" $
--     \mlfp mlfh -> do
--       callProcess "stack" ["exec", "agda-ocaml", "--", "-v0", "--mlf", agdap
--                           , "-o", mlfp]
-- runModFile' mlfp mlfh