convert value of InwSimpleVertex to float in C#

By Xiaodong Liang

Last week, we posted an article on how to get primitives from solid of Navisworks. We got a good question on how to convert value of InwSimpleVertex to float. The value of InwSimpleVertex (corrd) is defined as variant. Before Navisworks 2013, they can be got by an array like below:

public void Triangle(COMApi.InwSimpleVertex v1,                          COMApi.InwSimpleVertex v2,                          COMApi.InwSimpleVertex v3)    {        // do your work             // get the first value of the vertex             float fVal_nw2012_VS2010 = (float)(((Array)v1.coord)).GetValue(1);      }

 

However, this does not work with Navisworks 2013. You will receive an error ‘Unable to cast object of type ‘System.Single[]’ to type ‘System.Single[]’. The reason is all to do with the interaction between none ‘0 based’ SAFEARRAYS and recent .NET 4.0 C# dynamics. All our COM Api SAFEARRAYs  are 1 based, and the Dot NET dynamic system really doesn’t like this one little bit. Behind the scenes it makes a multi-dimensional Single[] array with just one dimension, it then tries to cast it to a single-dimensional Single[] array and thus explodes. The VS2010 debugger confuses things as it ‘debatably wrongly’ reports the array as Single[] rather than Single[*].  The solution is as below:

 

public void Triangle(COMApi.InwSimpleVertex v1,                          COMApi.InwSimpleVertex v2,                          COMApi.InwSimpleVertex v3)    {        // do your work             // get the first value of the vertex             Array array_v1 = (Array)(object)v1.coord;
<pre class="line-numbers"><code>float fVal_nw2013_VS2010 = (float)(array_v1.GetValue(1));</code></pre>    <div style="font-family: courier new;background: white;color: black;font-size: 8pt">     <p style="margin: 0px"><span style="color: blue">&#160;</span></p>      <p style="margin: 0px">}</p>   </div> </div>  <p>&#160;</p>  <p>Note: You must use GetValue() rather than [] indexer.</p>  <p>This is similar to other variant if it is an array such as InwLTransform3f. 

Comments

One response to “convert value of InwSimpleVertex to float in C#”

  1. Is there a way to access the behind-the-scene SAFEARRAYs?
    This would allow to significantly speed up these conversions safely.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading