帮我把一下lisp代码转成C#,实现相同的功能:(vl-load-com) ; 加载VL函数 (setq source_text nil) ; 设源文字为全局变量 (defun c:22 (/ en en_data en1 en1_data ent entype i ob pt ss ss_data) (setvar "cmdecho" 0) (setvar "ErrNo" 0) (command "_.undo" "begin") (if (= source_text nil) (if (setq en (nentsel "\n请选择源文字(右键退出):")) (setq en_data (entget (car en)) entype (cdr (assoc 0 en_data)) source_text (if (= entype "ATTDEF")(cdr (assoc 2 en_data))(cdr (assoc 1 en_data)));如果是属性字,则取“标记”为源文字 ) ) (if (and (= (setq en (nentsel (strcat "\n请选择源文字: 默认:" source_text))) nil) (= (getvar "ErrNo") 52)) (setvar "ErrNo" 0) (if en (setq en_data (entget (car en)) entype (cdr (assoc 0 en_data)) source_text (if (= entype "ATTDEF")(cdr (assoc 2 en_data))(cdr (assoc 1 en_data))) ) (setvar "ErrNo" 52) ) ) ) (prompt "\n请选择要修改内容的文字:") (while (/= (getvar "ErrNo") 52) (prompt (strcat "\n文字内容将被刷成:" source_text)) (if (and (setq ss (ssget ":S" '((0 . "*TEXT,TCH_DRAWINGNAME,TCH_ELEVATION,INSERT,ATTDEF,ATTRIB")))) source_text) (progn (if (= (caar (setq ss_data (ssnamex ss 0))) 1) (progn ; 点选时 (setq ent (ssname ss 0) pt (cadr (last (car ss_data))) en1 (car (nentselp pt)) en1_data (entget en1) entype (cdr (assoc 0 en1_data)) ob (vlax-ename->vla-object en1) ) (wenzishua entype ob source_text en1 ent) (princ) ) (progn ; 框选时 (setq i 0) (repeat (sslength ss) (setq en1 (ssname ss i) ent en1 en1_data (entget en1) entype (cdr (assoc 0 en1_data)) ob (vlax-ename->vla-object en1) ) (wenzishua entype ob source_text en1 ent) (setq i (1+ i)) ) ; end repeat ) ) ) ) ) ; end while (command ".undo" "_end") (setvar "ErrNo" 0) (setvar "cmdecho" 1) (princ) ) (defun wenzishua (entype ob source_text en1 ent) ; cad文字的内容格式刷 (if (or (= entype "TEXT") (= entype "MTEXT") ) (progn (vla-put-TextString ob source_text) (entupd en1) (entupd ent) ) ) ; 天正文字的内容格式刷 (if (or (= entype "TCH_TEXT") (= entype "TCH_ELEVATION") ) (progn (vlax-put-property ob 'Text source_text) (entupd en1) (entupd ent) ) ) ; 天正图名、标高的内容格式刷 (if (= entype "TCH_DRAWINGNAME") (progn (vlax-put-property ob 'NameText source_text) (entupd en1) (entupd ent) ) ) ; 属性文字 只改"标记" (if (= entype "ATTDEF") (progn (vla-put-TagString ob source_text);改标记 (entupd en1) (entupd ent) ) ) ; 块中属性文字 只改"默认" (if (= entype "ATTRIB") (progn (vla-put-TextString ob source_text);改默认 (entupd en1) (entupd ent) ) ) )
时间: 2023-12-01 10:02:09 浏览: 219
以下是将Lisp代码转换为C#的代码:
```csharp
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System.Collections.Generic;
public static class Program
{
private static string source_text = null;
[CommandMethod("22")]
public static void ChangeTextContent()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
ed.WriteMessage("\n请选择源文字(右键退出):");
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForAdding = "\n请选择源文字: 默认:" + source_text;
pso.RejectObjectsFromNonCurrentSpace = true;
pso.RejectObjectsOnLockedLayers = true;
pso.SingleOnly = true;
pso.AllowDuplicates = false;
pso.SinglePickInSpace = true;
pso.SingleOnly = true;
PromptSelectionResult psr = ed.GetSelection(pso);
if (psr.Status != PromptStatus.OK)
return;
if (source_text == null)
{
source_text = GetSourceText(psr.Value[0]);
}
else
{
psr = ed.GetSelection(pso);
if (psr.Status != PromptStatus.OK)
return;
source_text = GetSourceText(psr.Value[0]);
}
ed.WriteMessage("\n请选择要修改内容的文字:");
pso.MessageForAdding = "\n请选择要修改内容的文字:";
pso.SingleOnly = false;
psr = ed.GetSelection(pso);
if (psr.Status != PromptStatus.OK)
return;
SelectionSet ss = psr.Value;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (SelectedObject so in ss)
{
Entity ent = tr.GetObject(so.ObjectId, OpenMode.ForWrite) as Entity;
if (ent == null)
continue;
switch (ent.GetRXClass().DxfName)
{
case "TEXT":
case "MTEXT":
ent.UpgradeOpen();
DBText text = ent as DBText;
text.TextString = source_text;
ent.DowngradeOpen();
ent.Update();
break;
case "TCH_TEXT":
case "TCH_ELEVATION":
ent.UpgradeOpen();
MText mtext = ent as MText;
mtext.Contents = source_text;
ent.DowngradeOpen();
ent.Update();
break;
case "TCH_DRAWINGNAME":
ent.UpgradeOpen();
TcHText tchText = ent as TcHText;
tchText.NameText = source_text;
ent.DowngradeOpen();
ent.Update();
break;
case "ATTRIB":
ent.UpgradeOpen();
AttributeReference attRef = ent as AttributeReference;
if (attRef != null && attRef.Tag == "标记")
{
attRef.UpgradeOpen();
attRef.TagString = source_text;
attRef.DowngradeOpen();
}
ent.Update();
break;
case "INSERT":
BlockReference br = ent as BlockReference;
if (br != null)
{
foreach (ObjectId arId in br.AttributeCollection)
{
AttributeReference ar = tr.GetObject(arId, OpenMode.ForWrite) as AttributeReference;
if (ar == null)
continue;
if (ar.Tag == "标记")
{
ar.UpgradeOpen();
ar.TagString = source_text;
ar.DowngradeOpen();
ar.Update();
}
else if (ar.Tag == "默认")
{
ar.UpgradeOpen();
ar.TextString = source_text;
ar.DowngradeOpen();
ar.Update();
}
}
}
break;
default:
break;
}
}
tr.Commit();
}
}
private static string GetSourceText(ObjectId id)
{
string text = string.Empty;
using (Transaction tr = id.Database.TransactionManager.StartTransaction())
{
Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
if (ent == null)
return string.Empty;
switch (ent.GetRXClass().DxfName)
{
case "TEXT":
case "MTEXT":
DBText dbText = ent as DBText;
if (dbText != null)
text = dbText.TextString;
break;
case "TCH_TEXT":
case "TCH_ELEVATION":
MText mtext = ent as MText;
if (mtext != null)
text = mtext.Contents;
break;
case "TCH_DRAWINGNAME":
TcHText tchText = ent as TcHText;
if (tchText != null)
text = tchText.NameText;
break;
case "ATTDEF":
AttributeDefinition attDef = ent as AttributeDefinition;
if (attDef != null && attDef.Tag == "标记")
text = attDef.TagString;
break;
case "INSERT":
BlockReference br = ent as BlockReference;
if (br != null)
{
foreach (ObjectId arId in br.AttributeCollection)
{
AttributeReference ar = tr.GetObject(arId, OpenMode.ForRead) as AttributeReference;
if (ar == null)
continue;
if (ar.Tag == "标记")
{
text = ar.TagString;
break;
}
else if (ar.Tag == "默认")
{
text = ar.TextString;
break;
}
}
}
break;
default:
break;
}
tr.Commit();
}
return text;
}
}
```
请注意,由于Lisp和C#有很大的不同,因此在转换过程中,一些细节和逻辑可能会有所不同。
阅读全文