VB.NET 中正确使用GetExistin​gFacets

前端之家收集整理的这篇文章主要介绍了VB.NET 中正确使用GetExistin​gFacets前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
最近看到论坛中一个很有用的问题,谈到如何正确使用GetExistin​gFacets。原文地址

http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/GetExistingFacets-VBA-7-and-VB-NET-showsdifferent-and-fails-in/m-p/4314682/highlight/true#M44716

简述如下:

API帮助文档的例子ZHeightColors是用VBA写的,它首先通过GetExistin​gFacets获取当前实体的面片信息,基于此构造Client Graphics,然后沿着实体的包围盒Z方向,最小值位置设置为红色,最高值设置为蓝色,期间的颜色为渐变色。

VBA

  1. Public Sub ZHeightColors()
  2. ' Get the surface body from the active document.
  3. Dim oPartDoc As PartDocument
  4. Set oPartDoc = ThisApplication.ActiveDocument
  5. Dim oSurfBody As SurfaceBody
  6. Set oSurfBody = oPartDoc.ComponentDefinition.SurfaceBodies.Item(1)
  7. Set oSurfBody = oPartDoc.ComponentDefinitions.Item(1).SurfaceBodies.Item(1)
  8.  
  9. ' Delete the graphics data set and client graphics,if they exist.
  10. Dim oDataSets As GraphicsDataSets
  11. On Error Resume Next
  12. Set oDataSets = oPartDoc.GraphicsDataSetsCollection.Item("MyTest")
  13. If Err.Number = 0 Then
  14. oDataSets.Delete
  15. oPartDoc.ComponentDefinition.ClientGraphicsCollection.Item("MyTest").Delete
  16. oSurfBody.Visible = True
  17. ThisApplication.ActiveView.Update
  18. Exit Sub
  19. End If
  20. On Error GoTo 0
  21. ' Determine the highest tolerance of the existing facet sets.
  22. Dim ToleranceCount As Long
  23. Dim ExistingTolerances() As Double
  24. Call oSurfBody.GetExistingFacetTolerances(ToleranceCount,ExistingTolerances)
  25. Dim i As Long
  26. Dim BestTolerance As Double
  27. For i = 0 To ToleranceCount - 1
  28. If i = 0 Then
  29. BestTolerance = ExistingTolerances(i)
  30. ElseIf ExistingTolerances(i) < BestTolerance Then
  31. BestTolerance = ExistingTolerances(i)
  32. End If
  33. Next
  34. ' Get a set of existing facets.
  35. Dim iVertexCount As Long
  36. Dim iFacetCount As Long
  37. Dim adVertexCoords() As Double
  38. Dim adNormalVectors() As Double
  39. Dim aiVertexIndices() As Long
  40. Call oSurfBody.GetExistingFacets(BestTolerance,iVertexCount,iFacetCount,_
  41. adVertexCoords,adNormalVectors,aiVertexIndices)
  42. ' Start a transaction.
  43. Dim oTrans As Transaction
  44. Set oTrans = ThisApplication.TransactionManager.StartTransaction(oPartDoc,"Z Height Colors")
  45.  
  46. ' Create the graphics data sets collection.
  47. Set oDataSets = oPartDoc.GraphicsDataSetsCollection.Add("MyTest")
  48. ' Create the coordinate set and set it using the coordinates from the facets.
  49. Dim oGraphicsCoordSet As GraphicsCoordinateSet
  50. Set oGraphicsCoordSet = oDataSets.CreateCoordinateSet(1)
  51. Call oGraphicsCoordSet.PutCoordinates(adVertexCoords)
  52. ' Create the index set and set it using the indices from the facets.
  53. Dim oGraphicsIndexSet As GraphicsIndexSet
  54. Set oGraphicsIndexSet = oDataSets.CreateIndexSet(2)
  55. Call oGraphicsIndexSet.PutIndices(aiVertexIndices)
  56. ' Create the normal set and set it using the normals from the facets.
  57. Dim oGraphicsNormalSet As GraphicsNormalSet
  58. Set oGraphicsNormalSet = oDataSets.CreateNormalSet(3)
  59. Call oGraphicsNormalSet.PutNormals(adNormalVectors)
  60. ' Determine the min-max range of the body in Z.
  61. Dim dMinZ As Double
  62. dMinZ = oSurfBody.RangeBox.MinPoint.Z
  63. Dim dMaxZ As Double
  64. dMaxZ = oSurfBody.RangeBox.MaxPoint.Z
  65. Dim dHeightDifference As Double
  66. dHeightDifference = dMaxZ - dMinZ
  67. ' Allocate the array that will contain the color information.
  68. ' This array contains RGB values for each vertex.
  69. Dim abtColors() As Byte
  70. ReDim abtColors(0 To iVertexCount * 3 - 1) As Byte
  71. ' Load the array with color information for each vertex.
  72. For i = 0 To iVertexCount - 1
  73. ' Get the Z height of the current vertex.
  74. Dim dZValue As Double
  75. dZValue = adVertexCoords(i * 3 + 2)
  76. ' Set the color information for the current vertex. It's computed by
  77. ' determining the percentage of the total Z range of the body this vertex
  78. ' is within. A color between red and blue is computed based on this percentage.
  79. ' Blue is at the minimum Z and Red is at the maximum Z with blending between.
  80. abtColors(i * 3) = ((dZValue - dMinZ) / dHeightDifference) * 255
  81. abtColors(i * 3 + 1) = 0
  82. abtColors(i * 3 + 2) = ((dMaxZ - dZValue) / dHeightDifference) * 255
  83. Next
  84. ' Create the color set and set it using the array of rgb values just created.
  85. Dim oGraphicsColorSet As GraphicsColorSet
  86. Set oGraphicsColorSet = oDataSets.CreateColorSet(4)
  87. Call oGraphicsColorSet.PutColors(abtColors)
  88.  
  89. ' Create the client graphics collection.
  90. Dim oClientGraphics As ClientGraphics
  91. Set oClientGraphics = oPartDoc.ComponentDefinition.ClientGraphicsCollection.Add("MyTest")
  92. ' Create a graphics node.
  93. Dim oGraphicNode As GraphicsNode
  94. Set oGraphicNode = oClientGraphics.AddNode(1)
  95. ' Create the triangle graphics.
  96. Dim oTriangles As TriangleGraphics
  97. Set oTriangles = oGraphicNode.AddTriangleGraphics
  98. ' Set varIoUs prroperties of the triangle graphics.
  99. oTriangles.CoordinateSet = oGraphicsCoordSet
  100. oTriangles.CoordinateIndexSet = oGraphicsIndexSet
  101. oTriangles.NormalSet = oGraphicsNormalSet
  102. oTriangles.NormalBinding = kPerVertexNormals
  103. oTriangles.NormalIndexSet = oGraphicsIndexSet
  104. oTriangles.ColorSet = oGraphicsColorSet
  105. oTriangles.ColorBinding = kPerVertexColors
  106. oTriangles.ColorIndexSet = oGraphicsIndexSet
  107.  
  108. ' Turn off the display of the body.
  109. oSurfBody.Visible = False
  110. ' End the transaction.
  111. oTrans.End
  112.  
  113. ' Update the view.
  114. ThisApplication.ActiveView.Update
  115. End Sub

将此段代码贴到VB.NET,调整一些语法错误,编译通过。运行会发现GetExistin​gFacets 失败。这是为什么呢?

1. 首先是在.NET 中定义COM的整型数组,需要用Integers. MSDN是这样说的

If you are interfacing with components not written for the .NET Framework,for example Automation or COM objects,keep in mind that Long has a different data width (32 bits) in other environments. If you are passing a 32-bit argument to such a component,declare it as Integer instead of Long in your new Visual Basic code.

2. 注意数组以0为起始序号,而很多VBA的数组定义为:

Dim stuff(1 to 10) As Double


3. VB.NET中需要对数组初始化,而不能只是定义。例如

Dim adVertexCoords() As Double


需要变成


Dim adVertexCoords() As Double = {}

基于这些注意事项,以上的VBA代码修改如下后,就能成功运行了。

VB.NET

  1. Public Sub ZHeightColors()
  2. ' Get the surface body from the active document.
  3. Dim oPartDoc As PartDocument
  4. oPartDoc = m_invApp.ActiveDocument
  5. Dim oSurfBody As SurfaceBody
  6. oSurfBody = oPartDoc.ComponentDefinition.SurfaceBodies.Item(1)
  7. oSurfBody = oPartDoc.ComponentDefinitions.Item(1).SurfaceBodies.Item(1)
  8.  
  9. ' Delete the graphics data set and client graphics,if they exist.
  10. Dim oDataSets As GraphicsDataSets
  11. On Error Resume Next
  12. oDataSets = oPartDoc.GraphicsDataSetsCollection.Item("MyTest")
  13. If Err.Number = 0 Then
  14. oDataSets.Delete()
  15. oPartDoc.ComponentDefinition.ClientGraphicsCollection.Item("MyTest").Delete()
  16. oSurfBody.Visible = True
  17. m_invApp.ActiveView.Update()
  18. Exit Sub
  19. End If
  20. On Error GoTo 0
  21.  
  22. ' Determine the highest tolerance of the existing facet sets.
  23. Dim ToleranceCount As Integer
  24. Dim ExistingTolerances() As Double = {}
  25. Call oSurfBody.GetExistingFacetTolerances(ToleranceCount,ExistingTolerances)
  26.  
  27. Dim i As Integer
  28. Dim BestTolerance As Double
  29. For i = 0 To ToleranceCount - 1
  30. If i = 0 Then
  31. BestTolerance = ExistingTolerances(i)
  32. ElseIf ExistingTolerances(i) < BestTolerance Then
  33. BestTolerance = ExistingTolerances(i)
  34. End If
  35. Next
  36.  
  37. ' Get a set of existing facets.
  38. Dim iVertexCount As Integer
  39. Dim iFacetCount As Integer
  40. Dim adVertexCoords() As Double = {}
  41. Dim adNormalVectors() As Double = {}
  42. Dim aiVertexIndices() As Integer = {}
  43. Call oSurfBody.GetExistingFacets(BestTolerance,_
  44. adVertexCoords,aiVertexIndices)
  45.  
  46. ' Start a transaction.
  47. Dim oTrans As Transaction
  48. oTrans = m_invApp.TransactionManager.StartTransaction(oPartDoc,"Z Height Colors")
  49.  
  50. ' Create the graphics data sets collection.
  51. oDataSets = oPartDoc.GraphicsDataSetsCollection.Add("MyTest")
  52.  
  53. ' Create the coordinate set and set it using the coordinates from the facets.
  54. Dim oGraphicsCoordSet As GraphicsCoordinateSet
  55. oGraphicsCoordSet = oDataSets.CreateCoordinateSet(1)
  56. Call oGraphicsCoordSet.PutCoordinates(adVertexCoords)
  57.  
  58. ' Create the index set and set it using the indices from the facets.
  59. Dim oGraphicsIndexSet As GraphicsIndexSet
  60. oGraphicsIndexSet = oDataSets.CreateIndexSet(2)
  61. Call oGraphicsIndexSet.PutIndices(aiVertexIndices)
  62.  
  63. ' Create the normal set and set it using the normals from the facets.
  64. Dim oGraphicsNormalSet As GraphicsNormalSet
  65. oGraphicsNormalSet = oDataSets.CreateNormalSet(3)
  66. Call oGraphicsNormalSet.PutNormals(adNormalVectors)
  67.  
  68. ' Determine the min-max range of the body in Z.
  69. Dim dMinZ As Double
  70. dMinZ = oSurfBody.RangeBox.MinPoint.Z
  71. Dim dMaxZ As Double
  72. dMaxZ = oSurfBody.RangeBox.MaxPoint.Z
  73. Dim dHeightDifference As Double
  74. dHeightDifference = dMaxZ - dMinZ
  75.  
  76. ' Allocate the array that will contain the color information.
  77. ' This array contains RGB values for each vertex.
  78. Dim abtColors() As Byte
  79. ReDim abtColors(iVertexCount * 3 - 1)
  80.  
  81. ' Load the array with color information for each vertex.
  82. For i = 0 To iVertexCount - 1
  83. ' Get the Z height of the current vertex.
  84. Dim dZValue As Double
  85. dZValue = adVertexCoords(i * 3 + 2)
  86.  
  87. ' Set the color information for the current vertex. It's computed by
  88. ' determining the percentage of the total Z range of the body this vertex
  89. ' is within. A color between red and blue is computed based on this percentage.
  90. ' Blue is at the minimum Z and Red is at the maximum Z with blending between.
  91. abtColors(i * 3) = ((dZValue - dMinZ) / dHeightDifference) * 255
  92. abtColors(i * 3 + 1) = 0
  93. abtColors(i * 3 + 2) = ((dMaxZ - dZValue) / dHeightDifference) * 255
  94. Next
  95.  
  96. ' Create the color set and set it using the array of rgb values just created.
  97. Dim oGraphicsColorSet As GraphicsColorSet
  98. oGraphicsColorSet = oDataSets.CreateColorSet(4)
  99. Call oGraphicsColorSet.PutColors(abtColors)
  100.  
  101. ' Create the client graphics collection.
  102. Dim oClientGraphics As ClientGraphics
  103. oClientGraphics = oPartDoc.ComponentDefinition.ClientGraphicsCollection.Add("MyTest")
  104.  
  105. ' Create a graphics node.
  106. Dim oGraphicNode As GraphicsNode
  107. oGraphicNode = oClientGraphics.AddNode(1)
  108.  
  109. ' Create the triangle graphics.
  110. Dim oTriangles As TriangleGraphics
  111. oTriangles = oGraphicNode.AddTriangleGraphics
  112.  
  113. ' Set varIoUs prroperties of the triangle graphics.
  114. oTriangles.CoordinateSet = oGraphicsCoordSet
  115. oTriangles.CoordinateIndexSet = oGraphicsIndexSet
  116. oTriangles.NormalSet = oGraphicsNormalSet
  117. oTriangles.NormalBinding = NormalBindingEnum.kPerVertexNormals
  118. oTriangles.NormalIndexSet = oGraphicsIndexSet
  119. oTriangles.ColorSet = oGraphicsColorSet
  120. oTriangles.ColorBinding = ColorBindingEnum.kPerVertexColors
  121. oTriangles.ColorIndexSet = oGraphicsIndexSet
  122.  
  123. ' Turn off the display of the body.
  124. oSurfBody.Visible = False
  125.  
  126. ' End the transaction.
  127. oTrans.End()
  128.  
  129. ' Update the view.
  130. m_invApp.ActiveView.Update()
  131. End Sub

猜你在找的VB相关文章