<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Betreff: WblockCloneObjects() causes System.AccessViolationException in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3147244#M59423</link>
    <description>&lt;P&gt;So, I tried the thingy with creating new block-definitions first. However, the same error occors when I try to copy the elements into the block-definition.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I found something else:&lt;/P&gt;&lt;P&gt;I found out that the error comes when the drawings that I want to copy from (in this case the xrefs) are too large. For example when I run the proggi with an xref containing 4 lines it works ok. When I only change the xref to contain 624 lines and run the proggi in the host drawing again then I have the error.﻿&lt;/P&gt;</description>
    <pubDate>Tue, 06 Sep 2011 07:46:59 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2011-09-06T07:46:59Z</dc:date>
    <item>
      <title>WblockCloneObjects() causes System.AccessViolationException</title>
      <link>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3146630#M59420</link>
      <description>&lt;P&gt;Hi forum,&lt;BR /&gt;&lt;BR /&gt;As mentioned in the subject line, the database-method WblockCloneObjects() keeps causing a System.AccessViolationException with me.&lt;BR /&gt;&lt;BR /&gt;I try to copy the contents (entities) from a xref-database into my current space (which is open in the editor). Therefore I create a new DB and readDwgFile the xref-dwg-file in.&lt;BR /&gt;The whole thing is in a foreach loop. Unfortunately it is pretty unpredictory when it occurs. Sometimes it comes sooner, sometimes later. Sometimes the code works without any problems.&lt;BR /&gt;At the moment I tried to call WblockCloneObjects() outside of any transaction (therefore the somehow many transactions) but it did not help.&lt;BR /&gt;(I am sure the general coding-style could be better too. Please forgive &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://forums.autodesk.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;)&lt;BR /&gt;&lt;BR /&gt;Any ideas? Please help!!! Thx.﻿&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;            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 &amp;lt; 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 &amp;lt; 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;
            }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Sep 2011 08:17:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3146630#M59420</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-09-05T08:17:39Z</dc:date>
    </item>
    <item>
      <title>Betreff: WblockCloneObjects() causes System.AccessViolationException</title>
      <link>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3146674#M59421</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;first: do you have the code-line, where the code crashes? Or does AutoCAD bring you the exception after passing the whole code?&lt;/P&gt;&lt;P&gt;A few ideas:&lt;/P&gt;&lt;P&gt;1) When I interpret your code correctly, you will have a problem, when your XRef is placed more than once within the ModelSpace. You do (within the part "&lt;EM&gt;&lt;FONT color="#888888"&gt;Finally clone the entities for each reference of the current idBlkDef&lt;/FONT&gt;&lt;/EM&gt;﻿") multiply set the TransformMatrix before placing the objects on the ModelSpace, So having the XRef inserted 3 times, the TransformationMatrix of the last XRef wins.&lt;/P&gt;&lt;P&gt;2) Even when I don't read that correct, you do transform the objects in the XRef-DB ... I would not do that, do the transformation in the current database (with the already copied-objects, not with the source-objects).&lt;/P&gt;&lt;P&gt;3) my way of solving the issue (like binding XRef): create a Block(definition) and copy with WBlockCloneObjects the XRef-Objects not onto the ModelSpace of your current drawing, but directly into the new Block. Than you can insert this Block into the ModelSpace for every XRef-position and use the BlockTransformation of the XRef directly to the new BlockReference.&lt;/P&gt;&lt;P&gt;That would create less transformations (not every single entity has to be moved/scaled/rotated, one time the whole BlockReference, finished). And if you need the elements not in block-structure you can just explode the BlockReference at the end of your proggi.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTH, - alfred -&lt;/P&gt;</description>
      <pubDate>Mon, 05 Sep 2011 09:10:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3146674#M59421</guid>
      <dc:creator>Alfred.NESWADBA</dc:creator>
      <dc:date>2011-09-05T09:10:21Z</dc:date>
    </item>
    <item>
      <title>Betreff: WblockCloneObjects() causes System.AccessViolationException</title>
      <link>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3146760#M59422</link>
      <description>&lt;P&gt;Thx for the reply,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It always crashes at the lin:&lt;/P&gt;&lt;P&gt;sourceDb.WblockCloneObjects(﻿....);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ad 1) I just apply the matrix to the content of the xref-modelspace. And only the content is copied. When I copy it - let's say 3 times - the matrix is applied and kept for each time seperately, since the xref is "taken apart". This works. I know from the times when the code runs to the end without crashing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ad 2) Ok, I agree. However, even if I comment that section out completely, the crash still happens.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ad 3) Maybe that's better. I will have to try it out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Sep 2011 11:35:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3146760#M59422</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-09-05T11:35:28Z</dc:date>
    </item>
    <item>
      <title>Betreff: WblockCloneObjects() causes System.AccessViolationException</title>
      <link>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3147244#M59423</link>
      <description>&lt;P&gt;So, I tried the thingy with creating new block-definitions first. However, the same error occors when I try to copy the elements into the block-definition.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I found something else:&lt;/P&gt;&lt;P&gt;I found out that the error comes when the drawings that I want to copy from (in this case the xrefs) are too large. For example when I run the proggi with an xref containing 4 lines it works ok. When I only change the xref to contain 624 lines and run the proggi in the host drawing again then I have the error.﻿&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2011 07:46:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3147244#M59423</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-09-06T07:46:59Z</dc:date>
    </item>
    <item>
      <title>Betreff: WblockCloneObjects() causes System.AccessViolationException</title>
      <link>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3147260#M59424</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;just to make sure, you also have checked with _AUDIT that the source-db is ok, is free from errors?&lt;/P&gt;&lt;P&gt;I don't really think, that 600 objects (as long as they are not complex like ACA- oder CIVIL3D-objects) could be a handicap. In the case you can reproduce this, I would do as next step to split it to 100-packages, then to 50-packages .... maybe you find with this way one specific element, that causes the crash (btw. do you have proxies in the source-db?).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Good luck, - alfred -&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2011 07:54:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3147260#M59424</guid>
      <dc:creator>Alfred.NESWADBA</dc:creator>
      <dc:date>2011-09-06T07:54:38Z</dc:date>
    </item>
    <item>
      <title>Betreff: WblockCloneObjects() causes System.AccessViolationException</title>
      <link>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3147270#M59425</link>
      <description>&lt;P&gt;just found out that when I step through the code slowly (breakpoint and then manually) it works again.&lt;/P&gt;&lt;P&gt;But when I press the "continue" button too fast while debugging then it runs into the error again. *Ahhhhrrrghh*&lt;/P&gt;&lt;P&gt;Both with 624 elements.﻿&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2011 08:06:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3147270#M59425</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-09-06T08:06:22Z</dc:date>
    </item>
    <item>
      <title>Betreff: WblockCloneObjects() causes System.AccessViolationException</title>
      <link>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3147334#M59426</link>
      <description>&lt;P&gt;ok, it seems I finally found the solution (after probably a week of delay):&lt;BR /&gt;&lt;BR /&gt;The wrong part in&lt;BR /&gt;sourceDb.WblockCloneObjects(idsEntitiesToCopy, curDb.CurrentSpaceId, new IdMapping(), DuplicateRecordCloning.Replace, false);&lt;BR /&gt;is that I passed "new IdMapping()" directly to the calling of the method.&lt;BR /&gt;I MUST be defined outside first i.e. as&lt;BR /&gt;&lt;BR /&gt;IdMapping idm = new IdMapping();&lt;BR /&gt;&lt;BR /&gt;and then the method MUST be called with the variablename passed to it like this:&lt;BR /&gt;&lt;BR /&gt;sourceDb.WblockCloneObjects(idsEntitiesToCopy, curDb.CurrentSpaceId, idm, DuplicateRecordCloning.Replace, false);﻿&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2011 09:53:55 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/wblockcloneobjects-causes-system-accessviolationexception/m-p/3147334#M59426</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-09-06T09:53:55Z</dc:date>
    </item>
  </channel>
</rss>

