| |  | Gut zu wissen: Hilfreiche Tipps und Tricks aus der Praxis prägnant, und auf den Punkt gebracht für Autodesk Produkte | | |  | PNY präsentiert die neue NVIDIA RTX A400 und die A1000 Grafikkarte, eine Pressemitteilung
|
Autor
|
Thema: WblockCloneObjects() verursacht System.AccessViolationException (2347 mal gelesen)
|
autocart Mitglied Technischer Redakteur
  
 Beiträge: 698 Registriert: 08.09.2003
|
erstellt am: 05. Sep. 2011 10:31 <-- editieren / zitieren --> Unities abgeben:         
Hi Forum, Wie im Betreff erwähnt, verursacht die database-Methode WblockCloneObjects() bei mir eine System.AccessViolationException. Ich versuche, den Inhalt (Entities) einer Xref-database in meinen aktuell in Autocad offenen Bereich (Modell/Papier) herüberzukopieren. Dazu mache ich zuerst eine neue DB und lese die jeweilige Xref mit readDwgFile() ein. Das Ganze ist in einem foreach Loop. Leider ist es unberechenbar, wann der Fehler kommt. Manchmal früher, manchmal später und manchmal läuft das Proggi korrekt durch. Momentan probierte ich, WblockCloneObjects() außerhalb jeglicher Transaktion aufzurufen (dadurch die mehreren kurzen Transaktionen im Code), aber es hat nicht geholfen. (Ich bin mir sicher, dass auch der Coding-Stil an sich besser sein könnte. Ich bitte um Nachsicht ) Irgentwelche Ideen? Bitte um Hilfe!!! Danke. Code: try { foreach (ObjectId oId in blkDefsXref) { // Open xrefDb and copy its modelspace entities for each reference to the current space // // Load Xref into sideDb String xrefPath; BlockTableRecord btr; using (Transaction curTr = curDb.TransactionManager.StartTransaction()) { btr = curTr.GetObject(oId, OpenMode.ForRead) as BlockTableRecord; xrefPath = btr.PathName; curTr.Commit(); } if (xrefPath.Contains(".\\") | | !xrefPath.Contains("\\")) xrefPath = HostApplicationServices.Current.FindFile(xrefPath, curDb, FindFileHint.XRefDrawing); Database sourceDb = new Database(false, true); using (sourceDb) { sourceDb.ReadDwgFile(xrefPath, System.IO.FileShare.Read, true, ""); ObjectIdCollection idsEntitiesToCopy = new ObjectIdCollection(); using (Transaction sourceTr = sourceDb.TransactionManager.StartTransaction()) { // Get the entities which are to be cloned: // Open the source block table record Model space for read BlockTable sourceBlkTbl = sourceTr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord modelspace_source = sourceTr.GetObject(sourceBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord; // // Make objectIdCollection to be cloned (of entities in source modelspace) foreach (ObjectId tmpOId in modelspace_source) { DBObject tmpO = sourceTr.GetObject(tmpOId, OpenMode.ForRead); // The next line (if ...) filters the entites (typFilter_EntityClass was defined outside of the try-catch) if (tmpO.GetType() == typFilter_EntityClass | | tmpO.GetType().IsSubclassOf(typFilter_EntityClass)) idsEntitiesToCopy.Add(tmpOId); } sourceTr.Commit(); } // Finally clone the entities for each reference of the current idBlkDef: ObjectIdCollection oids = getReferencesOfBlockInCurrentSpace(btr); foreach (ObjectId xrefId in oids) { using (Transaction curTr = curDb.TransactionManager.StartTransaction()) { BlockReference xrefReference = curTr.GetObject(xrefId, OpenMode.ForRead) as BlockReference; // tranlate the entities to be copied so that they will be optically positioned on the same location in the drawing as the entities in the reference refOcsOrigin = xrefReference.Position; refOcsXaxis = xrefReference.GetPlane().GetCoordinateSystem().Xaxis; refOcsYaxis = xrefReference.GetPlane().GetCoordinateSystem().Yaxis; refOcsZaxis = xrefReference.Normal; refRotation = xrefReference.Rotation; refXscale = xrefReference.ScaleFactors.X; using (Transaction sourceTr = sourceDb.TransactionManager.StartTransaction()) { foreach (ObjectId entityOId in idsEntitiesToCopy) { Entity ent = sourceTr.GetObject(entityOId, OpenMode.ForWrite) as Entity; if (refXscale < 0) ent.TransformBy(Matrix3d.Mirroring(new Plane(sourceDb.Insbase, new Vector3d(0, 1, 0), new Vector3d(0, 0, 1)))); ent.TransformBy(Matrix3d.Rotation(refRotation, new Vector3d(0, 0, 1), sourceDb.Insbase)); ent.TransformBy(Matrix3d.AlignCoordinateSystem(sourceDb.Insbase, new Vector3d(1, 0, 0), new Vector3d(0, 1, 0), new Vector3d(0, 0, 1), refOcsOrigin, refOcsXaxis, refOcsYaxis, refOcsZaxis)); } sourceTr.Commit(); } curTr.Commit(); } sourceDb.WblockCloneObjects(idsEntitiesToCopy, curDb.CurrentSpaceId, new IdMapping(), DuplicateRecordCloning.Replace, false); // undo the translation using (Transaction sourceTr = sourceDb.TransactionManager.StartTransaction()) { foreach (ObjectId entityOId in idsEntitiesToCopy) { Entity ent = sourceTr.GetObject(entityOId, OpenMode.ForWrite) as Entity; ent.TransformBy(Matrix3d.AlignCoordinateSystem(refOcsOrigin, refOcsXaxis, refOcsYaxis, refOcsZaxis, sourceDb.Insbase, new Vector3d(1, 0, 0), new Vector3d(0, 1, 0), new Vector3d(0, 0, 1))); ent.TransformBy(Matrix3d.Rotation(refRotation * -1, new Vector3d(0, 0, 1), sourceDb.Insbase)); if (refXscale < 0) ent.TransformBy(Matrix3d.Mirroring(new Plane(sourceDb.Insbase, new Vector3d(0, 1, 0), new Vector3d(0, 0, 1)))); } sourceTr.Commit(); } } } } } catch (System.Exception e) { ed.WriteMessage("\nError in else (xref):\n" + e.ToString()); return; }
------------------ Gruß, Stephan www.stbartl.at "Alles sollte so einfach wie möglich gemacht werden, aber nicht einfacher." - Albert Einstein Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |

| |
autocart Mitglied Technischer Redakteur
  
 Beiträge: 698 Registriert: 08.09.2003 If you want to help and want to respect other people's questions, then, please: Please, ALWAYS assume that I already tried to search for an answer to my question. Thx. Also, please, DON'T ask me what the "actual" goal of my question is. Please, firstly just answer it directly as stated - if you know an answer and want to give it. Only secondly, add other commments. Many, many, many thanks in advance!
|
erstellt am: 06. Sep. 2011 12:01 <-- editieren / zitieren --> Unities abgeben:         
Nach vielleicht einer Woche Verzögerung durch diesen dummen Error habe ich nun das Problem endlich gefunden und lösen können (wie es z.Z. scheint): Das Problem bei der Zeile sourceDb.WblockCloneObjects(idsEntitiesToCopy, curDb.CurrentSpaceId, new IdMapping(), DuplicateRecordCloning.Replace, false); ist, dass ich "new IdMapping()" direkt im Methodenaufruf übergeben habe. Dies MUSS jedoch zuerst außerhalb definiert werden, z.B. so: IdMapping idm = new IdMapping(); und dann MUSS dieser Variablenname an den Methodenaufruf übergeben werden, z.B. so: sourceDb.WblockCloneObjects(idsEntitiesToCopy, curDb.CurrentSpaceId, idm, DuplicateRecordCloning.Replace, false); ------------------ Gruß, Stephan www.stbartl.at "Alles sollte so einfach wie möglich gemacht werden, aber nicht einfacher." - Albert Einstein Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
| Anzeige.:
Anzeige: (Infos zum Werbeplatz >>)
 |