From 519a2c67255cb16edced9ebfdc344c11fb7778b8 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Wed, 13 Mar 2019 00:18:59 -0400 Subject: add type signatures --- GenSCAD.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/GenSCAD.hs b/GenSCAD.hs index 6eca485..5989c65 100644 --- a/GenSCAD.hs +++ b/GenSCAD.hs @@ -30,13 +30,19 @@ _V32s c = intercalate "," (map show (_V32l c)) -- translate([1, 2, 3]) { cube([10, 20, 30]); } --crandrgb = intercalate "," +color :: [Char] -> [Char] -> [Char] color rgb what = "color ([" ++ rgb ++ "]) {" ++ what ++ "}" +translate :: [Char] -> [Char] -> [Char] translate to what = "translate([ " ++ to ++ "]) { " ++ what ++ " }" +cube :: [Char] -> [Char] cube d = "cube([ " ++ d ++ "]);" +cubeat :: [Char] -> [Char] -> [Char] cubeat d here = translate here $ (cube d) +colorcubeat :: [Char] -> [Char] -> [Char] -> [Char] colorcubeat rgb d pos = color rgb $ translate pos $ cube d --place [x] = "ERROR" +place :: [[Char]] -> [Char] place [d, p] = cubeat d p place [r, d, p] = colorcubeat r d p @@ -48,6 +54,7 @@ place [r, d, p] = colorcubeat r d p --scad [] = [] --scad obj = (place (head obj)) ++ "\n" ++ scad (tail obj) +scad :: [([Char], [Char])] -> [Char] scad [(d, p)] = cubeat p d --scad [(r, d, p)] = colorcubeat r d p --scad [x] = if (length x == 3) then (colorcubeat (x !! 0) (x !! 1) (x !! 2)) @@ -55,6 +62,7 @@ scad [(d, p)] = cubeat p d --scad (x:s) = scad [x] ++ scad s +placecube :: (R3 c2, R3 c3, Show a1, Show a2) => (c2 a1, c3 a2) -> [Char] 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) @@ -72,10 +80,14 @@ placecube obj = "translate([ " ++ _V32s(fst obj) ++ " ]) { cube([ " ++ _V32s(sn --scad (c, p, d) = colorcubeat c d p --scad (p,d) = cubeat d p +genscad :: (R3 c2, R3 c3, Show a1, Show a2) => [(c2 a1, c3 a2)] -> [Char] genscad objs = concat $ map (++ "\n") $ map (placecube) objs +printscad :: (R3 c2, R3 c3, Show a1, Show a2) => [(c2 a1, c3 a2)] -> IO () printscad objs = putStrLn $ genscad objs --writefilescad objs = withSystemTempFile "genscad" -> fp gsf -> doprintscad objs +writetempscad :: (R3 c2, R3 c3, Show a1, Show a2) => [(c2 a1, c3 a2)] -> IO FilePath writetempscad objs = writeSystemTempFile "genscad.scad" (genscad objs) +opentempscad :: (R3 c2, R3 c3, Show a1, Show a2) => [(c2 a1, c3 a2)] -> IO ProcessHandle opentempscad objs = do fp <- writetempscad objs runCommand $ "openscad " ++ fp -- cgit v1.2.3