Autodesk设计自动化

时间:2018-01-11 12:53:09

标签: automation autocad autodesk-forge

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

输出丢失

1 个答案:

答案 0 :(得分:0)

我最终在本地AutoCAD中使用HostApplicationServices.getRemoteFile,然后将其迁移到Design Automation。它现在也在运作。以下是.NET插件的命令。

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

我的演示ReadDWG远程URL文件中的实体,然后将实体wblock到当前绘图(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());
        } 

     }
  }
}