首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用clojure-clr作为脚本解释器

使用clojure-clr作为脚本解释器
EN

Stack Overflow用户
提问于 2014-11-18 11:18:53
回答 1查看 455关注 0票数 1

我在C#中设置了一个非常简单的clojure解释器,它加载一个.clj文件,并使这些函数可以在AutoCAD中使用。这很好用,但我想把它设置成更多的结构,这样我就可以‘模块化’源文件,而不是只有一个很棒的主文件(这是我目前唯一能让它工作的方法)。

我已经尝试了各种方法,如导入、使用、要求加载和加载脚本中的文件,以及在C#脚本代码中加载多个文件,但我宁愿有一个主脚本,当加载到解释器中时,它会引用其他文件中的引用。

这是我当前用来加载主文件的C#片段,

代码语言:javascript
复制
    Editor ed = _AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
    clojure.lang.Compiler.loadFile(AppEntry.AppPath + "..\\Scripts\\main.clj");
    PromptResult res = ed.GetString("Enter a clojure command: ");
    // res should have the user entered command to invoke:
    var foo = clojure.lang.RT.var("main", res.StringResult);
    object o = foo.invoke();

下面是我想要在运行时加载的两个文件的示例,主文件将引用所有其他文件,

代码语言:javascript
复制
(ns main) ;; the main file that gets loaded into interpreter

(import 
    '(Teigha.DatabaseServices Line)
    '(Teigha.Geometry Point3d)
    '(dbtools add-to-db)) ;; my other 'script' file I would like imported for use

(defn add-line 
    []
    (let [ line (Line. (Point3d. 20.0 20.0 0.0) (Point3d. 200.0 50.0 0.0))]
         ;; call an external script file method
         (add-to-db line))) 

还有我想引用的那个,目前与主文件在同一文件夹中,但希望在某个阶段将这些文件组织到子文件夹中。

代码语言:javascript
复制
(ns dbtools) ;; helper file/module

(import 
    '(Teigha.DatabaseServices Database SymbolUtilityServices 
                Transaction BlockTable BlockTableRecord OpenMode)
    '(Bricscad.ApplicationServices Application))

(defn add-to-db
    "Adds an AcDbEntity to ModelSpace of the current database
    Returns the ObjectId of the Entity added to the db."
    [entity]
    (let [ db (.. Application DocumentManager MdiActiveDocument Database)]
        (with-open [tr (.. db TransactionManager StartTransaction)]
            (let [  bt (.GetObject tr (.BlockTableId db) OpenMode/ForWrite)
                    btr(.GetObject tr (. SymbolUtilityServices GetBlockModelSpaceId db) OpenMode/ForWrite)]
                (let [id (.AppendEntity btr entity)]
                    (doto tr
                        (.AddNewlyCreatedDBObject entity true)
                        (.Commit))
                        id)))))

有没有关于最好的方法的指导?谢谢。

编辑:我让它处理主文件的以下更改,但我仍然可以通过更好的方法来做这件事,例如-如何设置加载路径以匹配main.clj文件夹。以下是更改后的文件以供参考:

代码语言:javascript
复制
(ns main) ;; the main file that gets loaded into interpreter

(load-file "C:\\path\\to\\dbtools.clj")
(require '[dbtools :as tools])

(import 
    '(Teigha.DatabaseServices Line)
    '(Teigha.Geometry Point3d))

(defn add-line []
    (let [ line (Line. (Point3d. 20.0 20.0 0.0) (Point3d. 200.0 50.0 0.0))]
         ;; call an external script file method
         (tools/add-to-db line)))
EN

回答 1

Stack Overflow用户

发布于 2015-03-19 15:46:08

对于Excel-REPL,我定义了以下助手函数。

代码语言:javascript
复制
(import System.Environment)
(require '[clojure.string :as string])

(defn get-load-path []
  (set (string/split (Environment/GetEnvironmentVariable "CLOJURE_LOAD_PATH") #";")))

(defn set-load-path! [s]
  (let [
        new-path (apply str (interpose ";" s))
        ]
    (Environment/SetEnvironmentVariable "CLOJURE_LOAD_PATH" new-path)
    new-path))

然后,我可以简单地更新加载路径

代码语言:javascript
复制
(set-load-path! (conj (get-load-path) "path/to/clojure/source/files"))

在那之后,我只需要输入clj文件,ClojureCLR就知道在哪里可以找到它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26986012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档