<?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 How to get transformation matrix of a nested block in WCS instead of OCS? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12286669#M7076</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to insert a block (Block_2) with the position and orientation being the same as another block (Block_1) . The block (Block_2) will be inserted in model space no matter the nesting of the block (Block_1)&lt;/P&gt;&lt;P&gt;If the Block_1 is nested (lets say in Block_3) , &lt;STRONG&gt;BlockRef.BlockTransform&lt;/STRONG&gt; returns the transformation matrix in OCS, and because of this the new block is being inserted in the wrong position.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to convert this transformation matrix from OCS to WCS?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for any help!.&lt;/P&gt;</description>
    <pubDate>Thu, 05 Oct 2023 12:21:20 GMT</pubDate>
    <dc:creator>ShricharanaB</dc:creator>
    <dc:date>2023-10-05T12:21:20Z</dc:date>
    <item>
      <title>How to get transformation matrix of a nested block in WCS instead of OCS?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12286669#M7076</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to insert a block (Block_2) with the position and orientation being the same as another block (Block_1) . The block (Block_2) will be inserted in model space no matter the nesting of the block (Block_1)&lt;/P&gt;&lt;P&gt;If the Block_1 is nested (lets say in Block_3) , &lt;STRONG&gt;BlockRef.BlockTransform&lt;/STRONG&gt; returns the transformation matrix in OCS, and because of this the new block is being inserted in the wrong position.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to convert this transformation matrix from OCS to WCS?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for any help!.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 12:21:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12286669#M7076</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2023-10-05T12:21:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to get transformation matrix of a nested block in WCS instead of OCS?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12288035#M7077</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;If you get the nested block with Editor.GetNestedEntity, the PromptNestedEntityResult provides a GetContainers method you can use to compute the transformation Matrix.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        private static Matrix3d GetTransformation(PromptNestedEntityResult promptResult)
        {
            var xform = Matrix3d.Identity;
            using (var tr = new OpenCloseTransaction())
            {
                if (promptResult.ObjectId.ObjectClass.Name == "AcDbAttribute")
                {
                    var attRef = (AttributeReference)tr.GetObject(promptResult.ObjectId, OpenMode.ForRead);
                    var owner = (BlockReference)tr.GetObject(attRef.OwnerId, OpenMode.ForRead);
                    xform = owner.BlockTransform;
                }
                foreach (ObjectId id in promptResult.GetContainers())
                {
                    var container = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                    xform = container.BlockTransform * xform;
                }
            }
            return xform;
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 05 Oct 2023 21:25:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12288035#M7077</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2023-10-05T21:25:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to get transformation matrix of a nested block in WCS instead of OCS?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12288488#M7078</link>
      <description>&lt;P&gt;Hi Gile,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the reply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;(Edit: Found out issue was something else. The below code works.)&lt;/P&gt;&lt;P&gt;I'm using &lt;STRONG&gt;editor.SelectAll&lt;/STRONG&gt; with "INSERT" filter to get the blocks and iterating through each block and the blocks nested in them as I need to attach the block_2 to Block_1 wherever it exists. I'm using a recursive method to do this for each block in the selection set. I tried the below but it added the blocks somewhere unintended.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;void mainMethod()
	{
		//getting database selection etc... 		
		foreach (SelectedObject sObj in ss)
		{
			BlockReference blockRef = (BlockReference)trans.GetObject(sObj.ObjectId, OpenMode.ForRead);
			RecusrsiveAttachBlock(blockRef, trans, Matrix3d.Identity);    
		}
	}

	void RecusrsiveAttachBlock(BlockReference blockRef, Transaction trans, Matrix3d parentTransform){
	{
		string blockName = blockRef.IsDynamicBlock ? ((BlockTableRecord)blockRef.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)).Name : blockRef.Name;
		if (ContainsString(BlockNames, blockName))
		{
			Matrix3d blockTransformationMatrix = parentTransform * blockRef.BlockTransform;
			//creating new block reference and setting block transform to blockTransformationMatrix	etc...
		}
		else
		{
			BlockTableRecord btr = (BlockTableRecord)trans.GetObject(blockRef.BlockTableRecord, OpenMode.ForRead);			
			parentTransform = parentTransform * blockRef.BlockTransform;
			foreach (ObjectId objID in btr)
			{
				Entity entity = trans.GetObject(objID, OpenMode.ForRead) as Entity;
				
				
				if (entity is BlockReference)
				{                        
					BlockReference br = trans.GetObject(objID, OpenMode.ForRead) as BlockReference;
					RecusrsiveAttachBlock(br, trans, parentTransform);
				}
			}
		}
		
	}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 12:40:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12288488#M7078</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2023-10-06T12:40:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to get transformation matrix of a nested block in WCS instead of OCS?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12288685#M7079</link>
      <description>&lt;P&gt;This one seems to work fine with a selected block reference.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        static IEnumerable&amp;lt;Matrix3d&amp;gt; GetNestedBlockTransform(BlockReference br, Matrix3d xform, string nestedBlockName)
        {
            xform *= br.BlockTransform;
            string blockName = ((BlockTableRecord)br.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)).Name;
            if (blockName == nestedBlockName)
            {
                yield return xform;
            }
            else
            {
                var btr = (BlockTableRecord)br.BlockTableRecord.GetObject(OpenMode.ForRead);
                var nestedBrs = btr
                    .Cast&amp;lt;ObjectId&amp;gt;()
                    .Where(id =&amp;gt; id.ObjectClass.Name == "AcDbBlockReference")
                    .Select(id =&amp;gt; (BlockReference)id.GetObject(OpenMode.ForRead));
                foreach (var nestedBr in nestedBrs)
                {
                    foreach (var matrix in GetNestedBlockTransform(nestedBr, xform, nestedBlockName))
                    {
                        yield return matrix;
                    }
                }
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;A testing command:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        [CommandMethod("TEST")]
        public static void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var filter = new SelectionFilter(new[] { new TypedValue(0, "INSERT") });
            var selection = ed.GetSelection(filter);
            if (selection.Status != PromptStatus.OK)
                return;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (bt.Has(blockToInsertName))
                {
                    foreach (var id in selection.Value.GetObjectIds())
                    {
                        var source = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                        var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        foreach (var xform in GetNestedBlockTransform(source, Matrix3d.Identity, nestedBlockName))
                        {
                            var br = new BlockReference(Point3d.Origin, bt[blockToInsertName]);
                            br.TransformBy(xform);
                            curSpace.AppendEntity(br);
                            tr.AddNewlyCreatedDBObject(br, true);
                        }
                    }

                }
                tr.Commit();
            }
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 06 Oct 2023 06:58:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12288685#M7079</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2023-10-06T06:58:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to get transformation matrix of a nested block in WCS instead of OCS?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12288931#M7080</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found the issue. The method I posted above works. I was using rotation after calculating the transform, for which I was providing&amp;nbsp;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Blockref.Position&lt;/STRONG&gt;&lt;/FONT&gt; as base point which should have been &lt;FONT color="#339966"&gt;transformationMatrix.CoordinateSystem3d.Origin&lt;/FONT&gt; . This was rotating the object with wrong base point and placing it somewhere else as a result.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 09:30:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12288931#M7080</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2023-10-06T09:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to get transformation matrix of a nested block in WCS instead of OCS?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12288975#M7081</link>
      <description>&lt;P&gt;Thank you for providing the snippet!&lt;/P&gt;&lt;P&gt;It will come in handy as I won't have to track and multiply the transformation matrices in recursive loops.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 09:50:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12288975#M7081</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2023-10-06T09:50:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to get transformation matrix of a nested block in WCS instead of OCS?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12289107#M7082</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/12961036"&gt;@ShricharanaB&lt;/a&gt;&amp;nbsp; a écrit&amp;nbsp;:&lt;BR /&gt;
&lt;P&gt;Thank you for providing the snippet!&lt;/P&gt;
&lt;P&gt;It will come in handy as I won't have to track and multiply the transformation matrices in recursive loops.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The GetNestedBlockTransform function is recursive.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 11:06:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12289107#M7082</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2023-10-06T11:06:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to get transformation matrix of a nested block in WCS instead of OCS?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12289306#M7083</link>
      <description>&lt;P&gt;Sorry, what I meant to say was that I won't have to handle the transformation matrix multiplication, or conversion to WCS, in loops I create to iterate through all blocks and nested blocks. I can just call the&amp;nbsp;&lt;SPAN&gt;GetNestedBlockTransform() and it'll handle that.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 12:38:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-transformation-matrix-of-a-nested-block-in-wcs/m-p/12289306#M7083</guid>
      <dc:creator>ShricharanaB</dc:creator>
      <dc:date>2023-10-06T12:38:57Z</dc:date>
    </item>
  </channel>
</rss>

