Commit 05495d Adding profile generation, fixed minor bugs.

7 files Authored and Committed by Tom Hawkins 6 years ago
Adding profile generation, fixed minor bugs.

    
 1 @@ -2,10 +2,12 @@
 2   module Language.Mecha
 3     ( module Language.Mecha.Export
 4     , module Language.Mecha.Solid
 5 +   , module Language.Mecha.Profile
 6     , module Language.Mecha.Types
 7     ) where
 8   
 9   import Language.Mecha.Export
10   import Language.Mecha.Solid
11 + import Language.Mecha.Profile
12   import Language.Mecha.Types
13   
1 @@ -79,7 +79,7 @@
2         primitive :: Primitive -> String
3         primitive a = case a of
4           Sphere d     -> printf "sphere(r = %f, $fn = 100);\n" (d / 2)
5 -         Cone bd td h -> printf "cylinder(h = %f, r1 = %f, r2 = %f, center = false, $fn = 100);\n" h (td / 2) (bd / 2)
6 +         Cone bd td h -> printf "cylinder(h = %f, r1 = %f, r2 = %f, center = false, $fn = 100);\n" h (bd / 2) (td / 2)
7           Box (x1, x2) (y1, y2) (z1, z2) -> printf "translate ([%f, %f, %f]) cube(size = [%f, %f, %f], center = false);\n" xmin ymin zmin (xmax - xmin) (ymax - ymin) (zmax - zmin)
8             where
9             xmin = min x1 x2
 1 @@ -0,0 +1,48 @@
 2 + -- | Generate profiles and cross sections of solid models.
 3 + module Language.Mecha.Profile
 4 +   ( profile
 5 +   , crossSection
 6 +   , Point
 7 +   , Line
 8 +   ) where
 9 + 
10 + import System.Directory
11 + import System.Process
12 + 
13 + import Language.Mecha.Export
14 + import Language.Mecha.Solid (Solid)
15 + 
16 + -- | Project profile to x-y plane and extract line data from OpenSCAD.
17 + profile :: Solid -> IO [Line]
18 + profile = cut False
19 + 
20 + -- | Capture cross section at x-y plane and extract line data form OpenSCAD.
21 + crossSection :: Solid -> IO [Line]
22 + crossSection = cut True
23 + 
24 + cut :: Bool -> Solid -> IO [Line]
25 + cut cut a = do
26 +   writeFile scad $ "projection(cut=" ++ (if cut then "true" else "false") ++ ")\n" ++ openSCAD a
27 +   readProcess "OpenSCAD" ["-o", dxf, scad] ""
28 +   f <- readFile dxf
29 +   if length f < 0 then undefined else return ()
30 +   removeFile scad
31 +   removeFile dxf
32 +   return $ parseDXF f
33 +   where
34 +   dxf  = "__mecha_input.dxf"
35 +   scad = "__mecha_output.scad"
36 + 
37 + type Point = (Double, Double)
38 + type Line  = (Point, Point)
39 + 
40 + -- | Extract the lines from an OpenSCAD DXF file.
41 + parseDXF :: String -> [Line]
42 + parseDXF = parse . lines
43 +   where
44 +   parse :: [String] -> [Line]
45 +   parse a = case a of
46 +     [] -> []
47 +     "LINE" : _ : _ : _ : x0 : _ : x1 : _ : y0 : _ : y1 : rest -> ((read x0, read y0), (read x1, read y1)) : parse rest
48 +     _ : rest -> parse rest
49 + 
1 @@ -92,6 +92,7 @@
2   cylinder' d h = moveZ (- h / 2) $ cylinder d h
3   
4   -- | A hollow cylinder with base at the origin, given outer diameter, inner diamter, and height.
5 + tube :: Double -> Double -> Double -> Solid
6   tube od id h = difference (cylinder od h) (moveZ (-h) $ cylinder id (4 * h))
7   
8   -- | A box with ranges or X, Y, and Z positions.
 1 @@ -1,14 +0,0 @@
 2 - module Main (main) where
 3 - 
 4 - import Language.Mecha
 5 - import Language.Mecha.Examples.CSG
 6 - 
 7 - main :: IO ()
 8 - main = do
 9 -   writeFile "csg.scad" $ openSCAD $ scaleAll 10 $ csg
10 -   putStrLn ""
11 -   putStrLn "Writing file: csg.scad"
12 -   putStrLn ""
13 -   putStrLn "Open with OpenSCAD, then click Design->Compile."
14 -   putStrLn ""
15 - 
 1 @@ -0,0 +1,14 @@
 2 + module Main (main) where
 3 + 
 4 + import System.Process
 5 + 
 6 + import Language.Mecha
 7 + import Language.Mecha.Examples.CSG
 8 + 
 9 + main :: IO ()
10 + main = do
11 +   putStrLn "Writing file csg.scad.  Opening with OpenSCAD ..."
12 +   writeFile "csg.scad" $ openSCAD $ scaleAll 10 $ csg
13 +   readProcess "OpenSCAD" ["csg.scad"] ""
14 +   return ()
15 + 
 1 @@ -1,5 +1,5 @@
 2   name:    mecha
 3 - version: 0.1.1
 4 + version: 0.1.2
 5   
 6   category: Language
 7   
 8 @@ -18,17 +18,20 @@
 9   homepage: http://tomahawkins.org
10   
11   build-type:    Simple
12 - cabal-version: >= 1.6
13 + cabal-version: >= 1.8
14   
15   library
16   
17       build-depends:
18 -         base       >= 4       && < 5
19 +         base,
20 +         directory,
21 +         process
22   
23       exposed-modules:
24           Language.Mecha
25           Language.Mecha.Examples.CSG
26           Language.Mecha.Export
27 +         Language.Mecha.Profile
28           Language.Mecha.Solid
29           Language.Mecha.Types
30   
31 @@ -36,6 +39,12 @@
32   
33   executable mecha-examples
34       main-is: MechaExamples.hs
35 +     hs-source-dirs: mains
36 +     build-depends:
37 +         base,
38 +         process,
39 +         mecha
40 + 
41    
42   source-repository head
43       type:     git