我有这个用于向 Autocad 发送命令的代码。它运行正常。
{
GetAcad() ;Creates global variable ACAD where Application Object is stored
CADdoc:= ACAD.activedocument
Layer:= "0"
CADdoc.SendCommand("_-LAYER _SET " %Layer% " `n`n") ;;Uses COM
CADdoc.SendCommand("_CHPROP _LA " %Layer% " `n`n") ;;Uses COM
sleep, 50
send, {Escape}
sleep, 50
send, {Escape}
return
}
尝试使用 Layer 参数创建函数
ACADChangeLayer("Layer_Name") ;This is how is the function called
ACADChangeLayer(Layer)
{
GetAcad()
global ACAD ;because I global variable has given value outside this function
ACAD.activedocument.SendCommand("_-LAYER _SET " %Layer% " `n`n")
ACAD.activedocument.SendCommand("_CHPROP _LA " %Layer% " `n`n")
sleep, 50
send, {Escape}
sleep, 50
send, {Escape}
}
没有按预期工作,试图找出原因......
ACADChangeLayer(Layer)
{
GetAcad()
global ACAD
msgbox, % acad.activedocument.name
msgbox, %Layer%
CommandSetActiveLayer:= ("_-LAYER _SET " %Layer% " `n`n")
msgbox, %CommandSetActiveLayer%
...
}
第一个 MsgBox 显示正确的 DocumentName (
drawing1.dwg
)第二个 MsgBoxu 显示正确的 LayerName(
Layer_Name
-参见上面的第二个代码块)第三个 MsgBox 仅显示变量之前的部分 (
"_-LAYER _SET "
)为什么?
谢谢你的建议。
答案1
问题在于赋值行应该写成以下之一:
CommandSetActiveLayer := "_-LAYER _SET " . Layer . "`n`n"
CommandSetActiveLayer := ("_-LAYER _SET " . Layer . "`n`n")
CommandSetActiveLayer = "_-LAYER _SET " %Layer% `n`n
前两行使用带有连接运算符(点)的较新的表达方法.
,而第二行使用传统方法。
参考 :变量和表达式。