Coder,
Now that the error is rectified,
I took a moment to look at what you were actually trying to achieve.
Your routine could be more economical. At the moment you are iterating the entire LayerTable and only acting in 2 instances.
The attached code will iterate the smaller list
LayerNames check if the layer exists and act accordingly.
This is considerably less processor intensive.
For giggles I also added an initializer message.
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
[assembly
: CommandClass
(typeof(FindLayer
.MyCommands))]
namespace FindLayer {
using System.Collections.Generic;
public class MyCommands {
[CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
public void MyCommand() {
var doc = AcadApp.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var db = doc.Database;
List
<string> layerNames
= new List
<string> { "Layer1",
"layer2" };
using(var tr = db.TransactionManager.StartTransaction()) {
try {
var layerTbl = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
foreach(string layerName in layerNames) {
if(layerTbl.Has(layerName)) {
var layerRecord = (LayerTableRecord)layerTbl[layerName].GetObject(OpenMode.ForWrite);
layerRecord.LineWeight = LineWeight.LineWeight005;
}
}
}
catch(System.Exception ex) {
ed.WriteMessage("\nError : " + ex.Message);
}
tr.Commit();
}
}
}
public class InitMyCommands : Autodesk.AutoCAD.Runtime.IExtensionApplication {
public void Initialize() {
AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
"\n\t\t\t*** type MyCommand to modify Layer1 and Layer2 LineWeight to 0.05 \t***");
}
public void Terminate() {
AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Cleaning up...");
}
}
}
Add Piccy: