欧特克设计自动化

autodesk design automation

致命错误:未处理的访问冲突在 1d8257a5h 读取 0x0008 异常

缺少输出失败

我终于让它在本地 AutoCAD 中与 HostApplicationServices.getRemoteFile 一起使用,然后将其迁移到 Design Automation。它现在也在工作。下面是.NET插件的命令

为了进行简单的测试,我在插件中硬编码了URL。您可以将 URL 替换为您身边的工作流程(通过 json 文件或设计自动化的输入参数)

我的演示从远程URL文件读取DWG实体,然后将实体写入当前图形(HostDWG),最后保存当前图形。

希望对解决您身边的问题有所帮助。

.NET 命令

namespace PackageNetPlugin
{   
    class DumpDwgHostApp: HostApplicationServices
    {
        public override string FindFile(string fileName, 
                                    Database database,
                                     FindFileHint hint)
        {
            throw new NotImplementedException();
        }

        public override string GetRemoteFile(Uri url, 
                                             bool ignoreCache)
        {
             //return base.GetRemoteFile(url, ignoreCache);

             Database db = 
             Autodesk.AutoCAD.ApplicationServices.Application.
                        DocumentManager.MdiActiveDocument.Database;

             string localPath = string.Empty; 
             if (ignoreCache)
             {
                 localPath = 
                    Autodesk.AutoCAD.ApplicationServices.Application.
                         GetSystemVariable("STARTINFOLDER") as string; 

                string filename = 
                    System.IO.Path.GetFileName(url.LocalPath);
                 localPath += filename;
                using (var client = new WebClient())
                {
                    client.DownloadFile(url, localPath);
                 }
            }  

            return localPath;
        }

        public override bool IsUrl(string filePath)
       {
            Uri uriResult;
            bool result = Uri.TryCreate(filePath, 
                          UriKind.Absolute, out uriResult)
                     && (uriResult.Scheme == Uri.UriSchemeHttp || 
                         uriResult.Scheme == Uri.UriSchemeHttps);

            return result;
        } 
 }

public class Class1
{  
    [CommandMethod("MyPluginCommand")]
    public void MyPluginCommand()
    {
        try { 
            string drawingPath = 
  @"https://s3-us-west-2.amazonaws.com/xiaodong-test-da/remoteurl.dwg";

            DumpDwgHostApp oDDA = new DumpDwgHostApp();
            string localFileStr = "";
            if (oDDA.IsUrl(drawingPath)){ 
                localFileStr = oDDA.GetRemoteFile(
                   new Uri(drawingPath), true);

            }

            if(!string.IsNullOrEmpty(localFileStr))
            {
                //source drawing from drawingPath
                Database source_db = new Database(false, true);
                source_db.ReadDwgFile(localFileStr, 
                      FileOpenMode.OpenTryForReadShare, false, null); 

                ObjectIdCollection sourceIds =
                           new ObjectIdCollection();
                using (Transaction tr = 
                    source_db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord btr = 
                       (BlockTableRecord)tr.GetObject(
                SymbolUtilityServices.GetBlockModelSpaceId(source_db), 
                                                    OpenMode.ForRead);
                    foreach (ObjectId id in btr)
                    {
                        sourceIds.Add(id);
                    }
                    tr.Commit();
                }

                //current drawing (main drawing working with workitem)
                Document current_doc =
                 Autodesk.AutoCAD.ApplicationServices.Application.
                         DocumentManager.MdiActiveDocument;
                Database current_db = current_doc.Database;  
                Editor ed = current_doc.Editor;   

                //copy the objects in source db to current db
                using (Transaction tr = 
                    current_doc.TransactionManager.StartTransaction())
                {
                    IdMapping mapping = new IdMapping();
                    source_db.WblockCloneObjects(sourceIds, 
        SymbolUtilityServices.GetBlockModelSpaceId(current_db), 
        mapping, DuplicateRecordCloning.Replace, false);
                    tr.Commit();
                }

            }

        }
        catch(Autodesk.AutoCAD.Runtime.Exception ex)
        {
            Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
        } 

     }
  }
}