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:
Leave a Reply