Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 148 lines (132 sloc) 5.435 kb
0d28b43 Christopher Olah Lots of work on the extend openscad format, adds a binary to run appropr...
authored
1 -- Implicit CAD. Copyright (C) 2011, Christopher Olah (chris@colah.ca)
2 -- Released under the GNU GPL, see LICENSE
3
5b806f5 Christopher Olah Better resolution default choices.
authored
4 {-# LANGUAGE ViewPatterns #-}
5
0d28b43 Christopher Olah Lots of work on the extend openscad format, adds a binary to run appropr...
authored
6 -- Let's make it convenient to run our extended openscad format code
7
bbacbfd Christopher Olah First step in refactoring based on awesome feedback at http://www.reddit...
authored
8 -- Let's be explicit about what we're getting from where :)
7bf74c6 Change the import System (getArgs) to System.Environment (getArgs) to ma...
Zac Slade authored
9 import System.Environment (getArgs)
bbacbfd Christopher Olah First step in refactoring based on awesome feedback at http://www.reddit...
authored
10 import System.IO (openFile, IOMode (ReadMode), hGetContents, hClose)
3ddcee5 Christopher Olah Adds a RayTracer.
authored
11 import Graphics.Implicit (runOpenscad, writeSVG, writeBinSTL, writeOBJ, writeSCAD3, writeSCAD2, writeGCodeHacklabLaser, writeTHREEJS, writePNG)
bbacbfd Christopher Olah First step in refactoring based on awesome feedback at http://www.reddit...
authored
12 import Graphics.Implicit.ExtOpenScad.Definitions (OpenscadObj (ONum))
17cbd69 Christopher Olah Better default resolution.
authored
13 import Graphics.Implicit.ObjectUtil (getBox2, getBox3)
b5122a0 Christopher Olah Move parse errors into new framework as well.
authored
14 import Graphics.Implicit.Definitions (xmlErrorOn, errorMessage)
bbacbfd Christopher Olah First step in refactoring based on awesome feedback at http://www.reddit...
authored
15 import Data.Map as Map
7a856d3 Christopher Olah Add a $quality variable for scale independent render quality.
authored
16 import Data.Maybe as Maybe
b5122a0 Christopher Olah Move parse errors into new framework as well.
authored
17 import Text.ParserCombinators.Parsec (errorPos, sourceLine)
18 import Text.ParserCombinators.Parsec.Error
5deb83a Christopher Olah Reworked error messaging.
authored
19 import Data.IORef (writeIORef)
ce811fa Christopher Olah A working, seperate program that renders (ext)openscad files into SVGs/S...
authored
20
bbacbfd Christopher Olah First step in refactoring based on awesome feedback at http://www.reddit...
authored
21 -- | strip a .scad or .escad file to its basename.
22 strip :: String -> String
ce811fa Christopher Olah A working, seperate program that renders (ext)openscad files into SVGs/S...
authored
23 strip filename = case reverse filename of
24 'd':'a':'c':'s':'.':xs -> reverse xs
25 'd':'a':'c':'s':'e':'.':xs -> reverse xs
26 _ -> filename
27
10ff68c Christopher Olah Improving command line argument handeler for extopenscad.
authored
28 -- | Get the file type ending of a file
29 -- eg. "foo.stl" -> "stl"
30 fileType filename = reverse $ beforeFirstPeriod $ reverse filename
31 where
32 beforeFirstPeriod [] = []
33 beforeFirstPeriod ('.':xs) = []
34 beforeFirstPeriod ( x:xs) = x : beforeFirstPeriod xs
35
5b806f5 Christopher Olah Better resolution default choices.
authored
36 getRes (Map.lookup "$res" -> Just (ONum res), _, _) = res
37
7a856d3 Christopher Olah Add a $quality variable for scale independent render quality.
authored
38 getRes (varlookup, _, obj:_) =
39 let
5b806f5 Christopher Olah Better resolution default choices.
authored
40 ((x1,y1,z1),(x2,y2,z2)) = getBox3 obj
41 (x,y,z) = (x2-x1, y2-y1, z2-z1)
7a856d3 Christopher Olah Add a $quality variable for scale independent render quality.
authored
42 in case Maybe.fromMaybe (ONum 1) $ Map.lookup "$quality" varlookup of
43 ONum qual | qual > 0 -> min (minimum [x,y,z]/2) ((x*y*z/qual)**(1/3) / 22)
44 _ -> min (minimum [x,y,z]/2) ((x*y*z )**(1/3) / 22)
5b806f5 Christopher Olah Better resolution default choices.
authored
45
7a856d3 Christopher Olah Add a $quality variable for scale independent render quality.
authored
46 getRes (varlookup, obj:_, _) =
47 let
5b806f5 Christopher Olah Better resolution default choices.
authored
48 ((x1,y1),(x2,y2)) = getBox2 obj
49 (x,y) = (x2-x1, y2-y1)
7a856d3 Christopher Olah Add a $quality variable for scale independent render quality.
authored
50 in case Maybe.fromMaybe (ONum 1) $ Map.lookup "$quality" varlookup of
51 ONum qual | qual > 0 -> min (min x y/2) ((x*y/qual)**0.5 / 30)
52 _ -> min (min x y/2) ((x*y )**0.5 / 30)
5b806f5 Christopher Olah Better resolution default choices.
authored
53
54 getRes _ = 1
55
bbacbfd Christopher Olah First step in refactoring based on awesome feedback at http://www.reddit...
authored
56 -- | Give an openscad object to run and the basename of
57 -- the target to write to... write an object!
58 executeAndExport :: String -> String -> IO ()
ce811fa Christopher Olah A working, seperate program that renders (ext)openscad files into SVGs/S...
authored
59 executeAndExport content targetname = case runOpenscad content of
b5122a0 Christopher Olah Move parse errors into new framework as well.
authored
60 Left err ->
61 let
62 line = sourceLine . errorPos $ err
63 msgs = errorMessages err
64 in errorMessage line $ showErrorMessages
65 "or" "unknown parse error" "expecting" "unexpected" "end of input"
66 (errorMessages err)
ce811fa Christopher Olah A working, seperate program that renders (ext)openscad files into SVGs/S...
authored
67 Right openscadProgram -> do
2335016 Christopher Olah Adding support for $res forcing a certain resolution in extopenscad.
authored
68 s@(vars, obj2s, obj3s) <- openscadProgram
10ff68c Christopher Olah Improving command line argument handeler for extopenscad.
authored
69 let
5b806f5 Christopher Olah Better resolution default choices.
authored
70 res = getRes s
10ff68c Christopher Olah Improving command line argument handeler for extopenscad.
authored
71 case s of
ce811fa Christopher Olah A working, seperate program that renders (ext)openscad files into SVGs/S...
authored
72 (_, [], []) -> putStrLn "Nothing to render"
73 (_, x:xs, []) -> do
74 putStrLn $ "Rendering 2D object to " ++ targetname ++ ".svg"
8b4adf4 Christopher Olah Refactoring and work on normals.
authored
75 putStrLn $ show x
2335016 Christopher Olah Adding support for $res forcing a certain resolution in extopenscad.
authored
76 writeSVG res (targetname ++ ".svg") x
ce811fa Christopher Olah A working, seperate program that renders (ext)openscad files into SVGs/S...
authored
77 (_, _, x:xs) -> do
10ff68c Christopher Olah Improving command line argument handeler for extopenscad.
authored
78 putStrLn $ "Rendering 3D object to " ++ targetname++ ".stl"
8b4adf4 Christopher Olah Refactoring and work on normals.
authored
79 putStrLn $ show x
c38ae58 Christopher Olah Binary STL now default output for extopenscad.
authored
80 writeBinSTL res (targetname ++ ".stl") x
10ff68c Christopher Olah Improving command line argument handeler for extopenscad.
authored
81
82 -- | Give an openscad object to run and the basename of
83 -- the target to write to... write an object!
84 executeAndExportSpecifiedTargetType :: String -> String -> String -> IO ()
85 executeAndExportSpecifiedTargetType content targetname formatname = case runOpenscad content of
86 Left err -> putStrLn $ show $ err
87 Right openscadProgram -> do
88 s@(vars, obj2s, obj3s) <- openscadProgram
89 let
5b806f5 Christopher Olah Better resolution default choices.
authored
90 res = getRes s
10ff68c Christopher Olah Improving command line argument handeler for extopenscad.
authored
91 case (formatname, s) of
92 (_, (_, [], [])) -> putStrLn "Nothing to render"
93 ("svg", (_, x:xs, _)) -> do
94 putStrLn $ "Rendering 2D object to " ++ targetname
95 writeSVG res targetname x
2d0218f Christopher Olah Some fixes for hacklab laser cutter gcode.
authored
96 ("ngc", (_, x:xs, _)) -> do
97 putStrLn $ "Rendering 2D object to " ++ targetname
98 writeGCodeHacklabLaser res targetname x
4c9dd0a Christopher Olah Initial commit of a SCAD output backend.
authored
99 ("scad", (_, x:xs, _)) -> do
3ddcee5 Christopher Olah Adds a RayTracer.
authored
100 putStrLn $ "Rendering 2D object to " ++ targetname
4c9dd0a Christopher Olah Initial commit of a SCAD output backend.
authored
101 writeSCAD2 res targetname x
10ff68c Christopher Olah Improving command line argument handeler for extopenscad.
authored
102 ("stl", (_, _, x:xs)) -> do
103 putStrLn $ "Rendering 3D object to " ++ targetname
c38ae58 Christopher Olah Binary STL now default output for extopenscad.
authored
104 writeBinSTL res targetname x
3ddcee5 Christopher Olah Adds a RayTracer.
authored
105 ("png", (_, _, x:xs)) -> do
106 putStrLn $ "Raytracing 3D object to " ++ targetname
107 writePNG res targetname x
4c9dd0a Christopher Olah Initial commit of a SCAD output backend.
authored
108 ("scad", (_, _, x:xs)) -> do
109 putStrLn $ "Rendering 3D object to " ++ targetname
110 writeSCAD3 res targetname x
10ff68c Christopher Olah Improving command line argument handeler for extopenscad.
authored
111 ("obj", (_, _, x:xs)) -> do
112 putStrLn $ "Rendering 3D object to " ++ targetname
113 writeOBJ res targetname x
692fe95 Christopher Olah Adding support for THREE.js code as a output format.
authored
114 ("js", (_, _, x:xs)) -> do
115 putStrLn $ "Rendering 3D object to " ++ targetname
116 writeTHREEJS res targetname x
10ff68c Christopher Olah Improving command line argument handeler for extopenscad.
authored
117 (otherFormat, _) -> putStrLn $ "Unrecognized format: " ++ otherFormat
6d21dd2 Christopher Olah Adding .obj format.
authored
118
53c954a Christopher Olah Add new Operations class for operations endangled in being 2D/3D or betw...
authored
119
ce811fa Christopher Olah A working, seperate program that renders (ext)openscad files into SVGs/S...
authored
120
bbacbfd Christopher Olah First step in refactoring based on awesome feedback at http://www.reddit...
authored
121 main :: IO()
0d28b43 Christopher Olah Lots of work on the extend openscad format, adds a binary to run appropr...
authored
122 main = do
123 args <- getArgs
5deb83a Christopher Olah Reworked error messaging.
authored
124 if Prelude.null args || args == ["--help"] || args == ["-help"]
125 then putStrLn $
10ff68c Christopher Olah Improving command line argument handeler for extopenscad.
authored
126 "syntax: extopenscad inputfile.escad [outputfile.format]\n"
127 ++ "eg. extopenscad input.escad out.stl"
5deb83a Christopher Olah Reworked error messaging.
authored
128 else do
129 let
130 args' = if head args == "-xml-error" then tail args else args
131 writeIORef xmlErrorOn (head args == "-xml-error")
132 case length args' of
133 0 -> putStrLn $
134 "syntax: extopenscad inputfile.escad [outputfile.format]\n"
135 ++ "eg. extopenscad input.escad out.stl"
136 1 -> do
137 f <- openFile (args' !! 0) ReadMode
138 content <- hGetContents f
139 executeAndExport content (strip $ args' !! 0)
140 hClose f
141 2 -> do
142 f <- openFile (args' !! 0) ReadMode
143 content <- hGetContents f
144 executeAndExportSpecifiedTargetType
145 content (args' !! 1) (fileType $ args' !! 1)
146 hClose f
147
Something went wrong with that request. Please try again.