乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 342|回复: 14

Clojure in Cad - 即将到来的事情的滋味

[复制链接]

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2014-11-2 22:18:42 | 显示全部楼层 |阅读模式
我今天一直在修补一些想法,并在Bricscad中获得了一个小的Clojure解释器,在这一点上非常粗略,并且是概念的证明。虽然很简单,您需要从以下位置下载clojureclr:https://github . com/clo jure/clo jure-clr/wiki/Getting-binaries 然后将clojureclr引用到项目中。要使用它,请创建一个C#类库项目并添加以下内容,根据需要更改您的库:   使用System。
使用系统,集合,泛型;。
使用系统,Linq。
使用系统,木卫一;。
使用系统,文本;。
使用系统,线程,任务;。


使用泰格哈,运行时;。
使用泰格哈,数据库服务;。
使用Bricscad,应用服务;。
使用Bricscad,EditorInput。

using _AcRx = Teigha,运行时;。
using _AcAp = Bricscad,应用服务;。
using _AcDb = Teigha,数据库服务;。
using _AcEd = Bricscad,EditorInput。

使用clojure。

[assembly:command class(type of(ClojureCad,命令))]。
命名空间ClojureCad。
{。
类命令。
{。
[command method(" clj CAD ")]。
公共静态void CljCad()。
{。
编辑ed = _AcAp,application . document manager . mdiactivedocument . editor;。
clo jure . lang . compiler . loadfile(" C:\ \ test CAD . clj ");。
提示结果res = ed,GetString("在\"\ ":")中输入clojure表达式;。
char[]trim chars = { ' " ' };。
string cljstring = RES . string result . trim(trim chars);。
object o = clo jure . lang . rt . read string(clj string);。
object result = clo jure . lang . compiler . eval(o);。
ed,WriteMessage("Result: " + result,ToString());。
}。

[command method(" clj cad1 ")]。
公共静态void CljCad1()。
{。
编辑ed = _AcAp,application . document manager . mdiactivedocument . editor;。
clo jure . lang . compiler . loadfile(" C:\ \ test CAD . clj ");。
提示结果res = ed,GetString("输入clojure命令:");。
var foo = clo jure . lang . rt . var(" clj CAD ",RES . string result);。
object o = foo . invoke();。
ed,write message(" Result:"+o . ToString());。
}。
}。
}。
下面是一个非常简单的Clojure脚本文件:
  1. (ns cljcad) ;; namespace for this file
  2. (defn command1 []
  3.   (str "Simple test to see clojure is connected!"))
  4.   
  5. (defn command2 []
  6.   (str "Another command, getting boring now :)"))
它是这样工作的:。

1udm4n4pcu4.jpg

1udm4n4pcu4.jpg

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2014-11-2 22:23:48 | 显示全部楼层
好吧,那里没有什么非常令人惊讶的...还没有。
真正的好处是我可以编辑我的脚本并再次运行它,而无需一直Netload,我可以进行快速编辑并再次运行它。
另一个好处是我可以使用任何. net框架以及ARX. net包装器,如果您对您的脚本感到满意,您可以将其编译为dll适当的并像任何其他类lib一样加载它。
在很早的阶段,我将尝试在接下来的db中添加一些东西,并尝试连接一个适当的repl,以便您可以即时编码并测试东西。
几乎没有帮助如果你感兴趣的话,它会上坡一段时间。
回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2014-11-2 23:34:45 | 显示全部楼层
进一步测试使用命令行的限制:
限制是在编辑器中使用括号或带引号的字符串时。GetString() Bricscad想要立即退出并将其解释为lisp表达式,有人能解决这个问题吗?
回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2014-11-3 01:57:46 | 显示全部楼层
嗨,米克,
很酷的东西!也许您可以使用LispFunction来代替get string代码1]
  1. (Clojure "(+ 1 3)" "(+ 3 9)")

回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2014-11-3 02:36:33 | 显示全部楼层

  1.     public static class MyClojureFuncs
  2.     {
  3.         static List loaded = new List();
  4.         //(LoadClojure "C:\\Temp\\testcad.clj")
  5.         [LispFunction("LoadClojure")]
  6.         public static object LoadClojure(ResultBuffer args)
  7.         {
  8.             foreach (var item in args)
  9.             {
  10.                 if ((LispDataType)item.TypeCode == LispDataType.Text)
  11.                 {
  12.                     loaded.Add(clojure.lang.Compiler.loadFile(item.Value.ToString()));
  13.                 }
  14.             }
  15.             return true;
  16.         }
  17.         //(CL "cljcad" "command1")
  18.         [LispFunction("CL")]
  19.         public static object CL(ResultBuffer args)
  20.         {
  21.             String func = String.Empty;
  22.             String space = String.Empty;
  23.             String result = String.Empty;
  24.             List _args = new List(args.AsArray());
  25.             if ((LispDataType)_args[0].TypeCode == LispDataType.Text)
  26.             {
  27.                 space = _args[0].Value.ToString();
  28.             }
  29.             if ((LispDataType)_args[1].TypeCode == LispDataType.Text)
  30.             {
  31.                 func = _args[1].Value.ToString();
  32.             }
  33.             var foo = clojure.lang.RT.var(space, func);
  34.             object o = foo.invoke();
  35.             return o.ToString();
  36.         }
  37.     }

:(LoadClojure"C:\\Temp\\testcad.clj")
T
:(CL"cljcad""命令1")
"简单的测试来查看clojure是否连接!"
回复

使用道具 举报

116

主题

996

帖子

9

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1466
发表于 2014-11-3 02:42:29 | 显示全部楼层
酷丹尼尔!谢谢你,我对LispFunction属性知之甚少,效果很好:
:(clj"(+3 4)")
("7")
:(clj"(deden my-add[n1 n2](+n1 n2)")
("#'clojure.core/my-add")
:(clj"(my-add 3 4)")
("7")
回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2014-11-3 04:30:58 | 显示全部楼层
这是到目前为止更新的脚本文件内容,它现在有一个添加行命令,喜欢这段代码的简洁性,但是导入有点拖拉
  1. (ns cljcad
  2.         (:import (Teigha.DatabaseServices Database SymbolUtilityServices Transaction BlockTable BlockTableRecord Line OpenMode))
  3.         (:import (Teigha.Geometry Point3d))
  4.         (:import (Bricscad.ApplicationServices Application)))
  5. (defn command1 []
  6.   (str "Simple test to see clojure is connected!"))
  7.   
  8. (defn command2 []
  9.   (str "Another command, getting boring now :)"))
  10. (defn- add-to-db ;; private function -> defn-
  11.         "Adds an AcDbEntity to ModelSpace of the current database"
  12.         [entity]
  13.         (let [ db (.. Application DocumentManager MdiActiveDocument Database)
  14.                         tr (.. db TransactionManager StartTransaction)
  15.                         bt (.GetObject tr (. db BlockTableId) OpenMode/ForWrite)
  16.                         btr(.GetObject tr (. SymbolUtilityServices GetBlockModelSpaceId db) OpenMode/ForWrite)
  17.                   ]
  18.         (let [id (.AppendEntity btr entity)]
  19.         (doto tr
  20.                 (.AddNewlyCreatedDBObject entity true)
  21.                 (.Commit)
  22.                 (.Dispose))
  23.                 id)))
  24.         
  25. (defn add-line
  26.         []
  27.         (let [ line (Line. (Point3d. 20.0 20.0 0.0) (Point3d. 200.0 50.0 0.0))]
  28.                  (add-to-db line)))
回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2014-11-3 04:35:29 | 显示全部楼层

我明白了,米克看起来很有意思。
回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2014-11-3 04:54:48 | 显示全部楼层

是凯丽!
我已经几个小时没有加载了,请保存脚本并重试。
虽然我花了几个小时来理解互操作,但它正在逐渐形成。
回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2014-11-4 01:05:41 | 显示全部楼层
这是最新的“脚本”文件内容,它可以清理一些东西,我对代码进行了注释,以解释Clojure中的一些宏和特殊表单函数,并解释它们是如何工作的。
  1. ;; Here we declare a namespace 'ns cljcad' and we 'import' the lib's we want to use
  2. ;; in our script. As we are using lib's from the host application we don't need to 'require' them.
  3. (ns cljcad
  4.         (:import (Teigha.DatabaseServices Database SymbolUtilityServices
  5.                                 Transaction BlockTable BlockTableRecord Line OpenMode))
  6.         (:import (Teigha.Geometry Point3d Vector3d))
  7.         (:import (Bricscad.ApplicationServices Application)))
  8. (defn command1 []
  9.   (str "Simple test to see clojure is connected!"))
  10.   
  11. (defn add-to-db
  12.         ;; This is a doc string, can be read from a repl with (doc fn-name)
  13.         "Adds an AcDbEntity to ModelSpace of the current database
  14.         Returns the ObjectId of the Entity added to the db."
  15.         [entity]
  16.         ;; get the database:
  17.         (let [ db (.. Application DocumentManager MdiActiveDocument Database)]
  18.                 ;; with-open is the same as 'using' in C#.
  19.                 ;; The '..' macro allows you to dig deep into multiple levels of class hierarchy.
  20.                 (with-open [tr (.. db TransactionManager StartTransaction)]
  21.                         ;; set up the block table and block table record objects:
  22.                         ;; The '.method-name' macro lets you call a method on the object (.method instanceObj)
  23.                         ;; The '. ' macro is like the '..' but only a single level deep
  24.                         ;; The '/' is for getting enums or static methods from an object - obj/enum or Class/staticMethod
  25.                         (let [  bt (.GetObject tr (.BlockTableId db) OpenMode/ForWrite)
  26.                                         btr(.GetObject tr (. SymbolUtilityServices GetBlockModelSpaceId db) OpenMode/ForWrite)]
  27.                                 ;; here we bind the returned object id from AppendEntity to return later:
  28.                                 (let [id (.AppendEntity btr entity)]
  29.                                         ;; 'doto' is like chaining actions to the object we are using, a transaction in this case.
  30.                                         (doto tr
  31.                                                 (.AddNewlyCreatedDBObject entity true)
  32.                                                 (.Commit))
  33.                                                 id))))) ;; returns the object id
  34.                                                 
  35. ;; Typical function definition:
  36. ;; 'defn' is a special form to denote function creation,
  37. ;; params passed to the function are within '[]' and you can have more than one 'arity'
  38. ;; of params (think overloading). You may then have a doc string and then the body of the function.
  39. ;; An example for the function below with multiple arity might be -
  40. ;; (defn add-line
  41. ;;              []              ;; no args
  42. ;;              [p1 p2]         ;; 2 args passed in
  43. ;;              ;;body that uses the args passed or uses defaults
  44. ;;              ( ... ))
  45. (defn add-line
  46.         []
  47.         ;; To create new instances of objects we can use (Obj. args) or (new Obj args)
  48.         (let [ line (Line. (Point3d. 20.0 20.0 0.0) (Point3d. 200.0 50.0 0.0))]
  49.                  ;; add it to the db:
  50.                  (add-to-db line)))
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-2-4 16:31 , Processed in 0.225361 second(s), 75 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表