xk2005 发表于 2025-1-12 00:14:48

Teigha报错求助大神

c#编了一段复制模板到目标图的代码。GC进程总是报错        System.AccessViolationException。
protected unsafe override void DeleteUnmanagedObject()
        {
                OdRxObject* impObj = GetImpObj();
                ((delegate* unmanaged<IntPtr, void>)(int)(*(uint*)(*(int*)impObj + 20)))((nint)impObj);
        }
就是这段代码报错。
下面是我的代码,求大神帮忙分析一下到底哪里有问题,该怎么改。万分感谢
      private bool GenerateDwgFiles(Dictionary<string, List<List<(string Weihehao, string Shebeiguandaohao, List<(string Jianhao, string Daima, string Mingcheng, string Guige, string Cailiao, string Shuliang, string Beizhu)>)>>> dataByAnzhuangtuhao, string outputFile)
      {
            if (File.Exists(outputFile))
            {
                File.Delete(outputFile);
            }

            double offsetX = 0;
            double offsetY = 0;
            int columns = 4;
            double sheetWidth = 210;
            double sheetHeight = 297;
            double horizontalSpacing = 10;
            double verticalSpacing = 10;
            int globalIndex = 0; // 全局序号
            string fname = "D:\\output\\file0.dwg";

            using (Services svc = new Services())
            {
                using (Database db = new Database(false, false))
                {
                  db.ReadDwgFile(fname, FileShare.Read, false, null);

                  foreach (var kvp in dataByAnzhuangtuhao)
                  {
                        string anzhuangtuhao = kvp.Key;
                        string templateFile = Path.Combine(templatePath, anzhuangtuhao + ".dwg");

                        List<List<(string Weihehao, string Shebeiguandaohao, List<(string Jianhao, string Daima, string Mingcheng, string Guige, string Cailiao, string Shuliang, string Beizhu)>)>> pages = kvp.Value;

                        if (!File.Exists(templateFile))
                        {
                            MessageBox.Show($"模板文件未找到: {templateFile}");
                            continue;
                        }

                        int totalPages = pages.Count;
                        int rows = (int)Math.Ceiling((double)totalPages / columns);

                        for (int row = 0; row < rows; row++)
                        {
                            for (int col = 0; col < columns; col++)
                            {
                              int pageIndex = row * columns + col;
                              if (pageIndex >= totalPages) break;

                              CopyTemplateToDestDB(db, templateFile, ref offsetX, ref offsetY, row, col, sheetWidth, sheetHeight, horizontalSpacing, verticalSpacing);
                            }
                        }
                  }

                  db.SaveAs(outputFile, DwgVersion.Current);
                  db.Dispose();
                }
            }

            return true;
      }

      /// <summary>
      /// 复制模板内容到目标数据库
      /// </summary>
      private void CopyTemplateToDestDB(Database destDB, string templateFile, ref double offsetX, ref double offsetY, int row, int col, double sheetWidth, double sheetHeight, double horizontalSpacing, double verticalSpacing)
      {
            using (Database srcDB = new Database(false, false))
            {
                srcDB.ReadDwgFile(templateFile, System.IO.FileShare.Read, false, null);

                using (Transaction srcTrans = srcDB.TransactionManager.StartTransaction())
                {
                  BlockTable blockTable = srcTrans.GetObject(srcDB.BlockTableId, OpenMode.ForRead) as BlockTable;
                  BlockTableRecord modelSpace = srcTrans.GetObject(blockTable, OpenMode.ForWrite) as BlockTableRecord;

                  // 计算偏移量
                  offsetX = col * (sheetWidth + horizontalSpacing);
                  offsetY = -row * (sheetHeight + verticalSpacing);

                  // 获取模型空间中的所有对象
                  ObjectIdCollection pObjIDsColl = new ObjectIdCollection();
                  foreach (ObjectId objId in modelSpace)
                  {
                        pObjIDsColl.Add(objId);
                  }

                  // 对每个对象进行平移
                  foreach (ObjectId objId in pObjIDsColl)
                  {
                        Entity entity = srcTrans.GetObject(objId, OpenMode.ForWrite) as Entity;
                        if (entity != null)
                        {
                            Matrix3d translation = Matrix3d.Displacement(new Vector3d(offsetX, offsetY, 0));
                            entity.TransformBy(translation);
                        }
                  }

                  srcTrans.Commit();

                  // 将模板文件中的内容复制到目标数据库
                  using (Transaction destTrans = destDB.TransactionManager.StartTransaction())
                  {
                        BlockTable destBTable = destTrans.GetObject(destDB.BlockTableId, OpenMode.ForRead) as BlockTable;
                        ObjectId destModelSpaceId = destBTable;
                        IdMapping pIdMap = new IdMapping();

                        // 克隆对象到目标数据库
                        srcDB.WblockCloneObjects(pObjIDsColl, destModelSpaceId, pIdMap, DuplicateRecordCloning.Replace, false);
                        destTrans.Commit();
                  }
                }
            }
      }


页: [1]
查看完整版本: Teigha报错求助大神