;; Here we declare a namespace 'ns cljcad' and we 'import' the lib's we want to use
;; in our script. As we are using lib's from the host application we don't need to 'require' them.
(ns cljcad
(:import (Teigha.DatabaseServices Database SymbolUtilityServices
Transaction BlockTable BlockTableRecord Line OpenMode))
(:import (Teigha.Geometry Point3d Vector3d))
(:import (Bricscad.ApplicationServices Application)))
(defn command1 []
(str "Simple test to see clojure is connected!"))
(defn add-to-db
;; This is a doc string, can be read from a repl with (doc fn-name)
"Adds an AcDbEntity to ModelSpace of the current database
Returns the ObjectId of the Entity added to the db."
[entity]
;; get the database:
(let [ db (.. Application DocumentManager MdiActiveDocument Database)]
;; with-open is the same as 'using' in C#.
;; The '..' macro allows you to dig deep into multiple levels of class hierarchy.
(with
-open [tr
(.. db TransactionManager StartTransaction
)] ;; set up the block table and block table record objects:
;; The '.method-name' macro lets you call a method on the object (.method instanceObj)
;; The '. ' macro is like the '..' but only a single level deep
;; The '/' is for getting enums or static methods from an object - obj/enum or Class/staticMethod
(let [ bt (.GetObject tr (.BlockTableId db) OpenMode/ForWrite)
btr(.GetObject tr (. SymbolUtilityServices GetBlockModelSpaceId db) OpenMode/ForWrite)]
;; here we bind the returned object id from AppendEntity to return later:
(let [id (.AppendEntity btr entity)]
;; 'doto' is like chaining actions to the object we are using, a transaction in this case.
(doto tr
(.AddNewlyCreatedDBObject entity true)
(.Commit))
id))))) ;; returns the object id
;; Typical function definition:
;; 'defn' is a special form to denote function creation,
;; params passed to the function are within '[]' and you can have more than one 'arity'
;; of params (think overloading). You may then have a doc string and then the body of the function.
;; An example for the function below with multiple arity might be -
;; (defn add-line
;; [] ;; no args
;; [p1 p2] ;; 2 args passed in
;; ;;body that uses the args passed or uses defaults
;; ( ... ))
(defn add-line
[]
;; To create new instances of objects we can use (Obj. args) or (new Obj args)
(let [ line (Line. (Point3d. 20.0 20.0 0.0) (Point3d. 200.0 50.0 0.0))]
;; add it to the db:
(add-to-db line)))