Message 1 of 3
New to .NET - Loading and Running an Application
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm just starting out with .NET and am trying to get this code (from Norman Yuan) to run:
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using ConsoleApp1;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(MyCommands))]
namespace ConsoleApp1
{
public class MyCommands
{
[CommandMethod("ListDataLinks")]
public static void GetDataLinks()
{
var dwg = CadApp.DocumentManager.MdiActiveDocument;
var ed = dwg.Editor;
try
{
var links = GetDataLinksInDb(dwg.Database);
if (links.Count() > 0)
{
ed.WriteMessage("\nExcel Data Link(s) found in current drawing:");
foreach (var link in links)
{
ed.WriteMessage($"\n\t{link}");
}
ed.WriteMessage("\n");
}
else
{
ed.WriteMessage("\nNo Excel Data Link found in current drawing.\n");
}
}
catch (System.Exception ex)
{
ed.WriteMessage("\nError:\n{0}.", ex.Message);
}
finally
{
Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
}
}
[CommandMethod("UpdateDataLink")]
public static void RepathDataLink()
{
var dwg = CadApp.DocumentManager.MdiActiveDocument;
var ed = dwg.Editor;
string linkName = "JEI - Cable";
string newSource = @"C:\PRD\Apps\LISP\Datalink Test\Data Link Test 002.xlsx";
using (var tran = dwg.TransactionManager.StartTransaction())
{
var link = GetDataLink(linkName, dwg.Database, tran);
if (link != null)
{
var source = link.ConnectionString;
CadApp.ShowAlertDialog($"Data Link: \"{linkName}\"" +
$"\nCurrent Link Source: \"{source}\"");
var suffix = "";
var pos = source.IndexOf("!");
if (pos > 0)
{
suffix = source.Substring(pos + 1);
}
if (!string.IsNullOrEmpty(suffix))
{
newSource = newSource + "!" + suffix;
}
if (!link.IsWriteEnabled) link.UpgradeOpen();
link.ConnectionString = newSource;
link.Update(UpdateDirection.SourceToData, UpdateOption.AllowSourceUpdate);
}
tran.Commit();
}
}
private static IEnumerable<string> GetDataLinksInDb(Database db)
{
var links = new List<string>();
using (var tran = db.TransactionManager.StartTransaction())
{
var dic = (DBDictionary)tran.GetObject(db.DataLinkDictionaryId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
foreach (DBDictionaryEntry entry in dic)
{
links.Add(entry.Key);
}
tran.Commit();
}
return links;
}
private static DataLink GetDataLink(string linkName, Database db, Autodesk.AutoCAD.DatabaseServices.Transaction tran)
{
DataLink link = null;
var dic = (DBDictionary)tran.GetObject(db.DataLinkDictionaryId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
foreach (DBDictionaryEntry entry in dic)
{
if (entry.Key.ToUpper() == linkName.ToUpper())
{
link = (DataLink)tran.GetObject(entry.Value, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
break;
}
}
return link;
}
}
}
It builds with no errors and I think it's loading when I NETLOAD it, but I get an "Unkown Command" when I run a command (UpdateDataLink) from the code.
Not sure of what I'm missing...?
AutoCad 2018 (full)
Win 11 Pro
Win 11 Pro