Skip to content

Commit

Permalink
Merge pull request #173 from kaveh808/Kaveh-devel-4
Browse files Browse the repository at this point in the history
STL format export
  • Loading branch information
kaveh808 authored Nov 8, 2022
2 parents fec11ee + dce6e8f commit 9f9c1ad
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions kons-9.asd
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
(:file "src/plugins/poly-mesh")
(:file "src/plugins/usd")
(:file "src/plugins/obj")
(:file "src/plugins/stl")
))

(asdf:defsystem #:kons-9/testsuite
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/obj.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
(defparameter *obj-export-vref-offset* 1)

(defmethod export-obj ((scene scene) filename)
(with-open-file (stream filename :direction :output :if-exists :overwrite :if-does-not-exist :create)
(with-open-file (stream filename :direction :output :if-exists :supersede :if-does-not-exist :create)
(write-obj scene stream)))

(defmethod write-obj ((scene scene) &optional (stream t) (matrix nil))
Expand Down
41 changes: 41 additions & 0 deletions src/plugins/stl.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(in-package #:kons-9)

;;;; stl format export =========================================================

(defmethod export-stl ((polyh polyhedron) filename)
(with-open-file (stream filename :direction :output :if-exists :supersede :if-does-not-exist :create)
(write-stl polyh stream)))

(defmethod write-stl ((polyh polyhedron) &optional (stream t))
(format stream "solid ~A -- kons-9 stl export -- https://github.com/kaveh808/kons-9~%" (name polyh))
(multiple-value-bind (lo hi)
(get-bounds polyh)
(declare (ignore hi))
;; move polyh so no negative coords, and then triangulate it
(let ((tri-polyh (freeze-transform (translate-by (triangulate-polyhedron polyh) (p:negate lo)))))
;; export data
(do-array (i f (faces tri-polyh))
(let ((n (aref (face-normals tri-polyh) i)))
(when (not (p:= n +origin+)) ;skip degenerate triangles
(format stream " facet normal ~E ~E ~E~%" (p:x n) (p:y n) (p:z n)) ;scientific notation
(format stream " outer loop~%")
(dolist (vref f)
(let ((v (aref (points tri-polyh) vref)))
(format stream " vertex ~E ~E ~E~%" (p:x v) (p:y v) (p:z v)))) ;scientific notation
(format stream " endloop~%")
(format stream " endfacet~%")))))
(format stream "endsolid ~A~%" (name polyh))))




;;; stl export test

#|
(export-stl (make-superquadric 16 16 20.0 0.2 0.2) "~/superq.stl")
(export-stl (make-cube 20) "~/cube.stl")
|#




0 comments on commit 9f9c1ad

Please sign in to comment.