Variable type Compiler Error Please Help

Value of type 1-dimensional array of NXOpen.Annotations.PmiCollection cannot be converted to 1-dimensional array of NXOpen.DisplayableObject because NXOpen.Annotations.Pmi is not derived from NXOpen.DisplayableObject


Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen

Module Module9

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow

Dim PmiLayer As Integer = 69
Dim SketchLayer As Integer = 30

Dim displayModification1 As DisplayModification
Dim displayModification2 As DisplayModification

displayModification1 = theSession.DisplayManager.NewDisplayModification()
displayModification1.NewLayer = PmiLayer

displayModification2 = theSession.DisplayManager.NewDisplayModification()
displayModification2.NewLayer = SketchLayer

'displayModification1.Apply(workPart.Annotations.Centerlines.ToArray)
'displayModification1.Apply(workPart.Annotations.Datums.ToArray)

THIS LINE IS WHERE THE ERROR IS THROWN
displayModification1.Apply(theSession.Parts.Work.PmiManager.Pmis.ToArray)

displayModification2.Apply(theSession.Parts.Work.Sketches.ToArray)

'displayModification2.Apply(theSession.Parts.Work.Features.ToArray)

displayModification1.Dispose()

End Sub

End Module

I don't often use the PMI objects, but there is a .GetDisplayInstances method that looks like it will return the graphical representation of the PMI object. You cannot use the PMI objects directly in this case because they do not inherit from the DisplayableObject class, which is a required input for the display modification object.

Ah so just like grabbing sketch features from each sketch I need to grab display instances from each set of PMI?

I'm getting another error for the following block of code.

System.InvalidCastException: Unable to cast object of type 'NXOpen.Features.DatumCsys' to type NXOpen.DisplayableObject'.


Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen

Module Module9

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
Dim sketchToMove As New List(Of DisplayableObject)
Dim featureToMove As New List(Of DisplayableObject)

Dim PmiLayer As Integer = 69
Dim SketchLayer As Integer = 20
Dim featureLayer As Integer = 35

Dim displayModification1 As DisplayModification
Dim displayModification2 As DisplayModification
Dim displayModification3 As DisplayModification

displayModification1 = theSession.DisplayManager.NewDisplayModification()
displayModification1.NewLayer = PmiLayer

displayModification2 = theSession.DisplayManager.NewDisplayModification()
displayModification2.NewLayer = SketchLayer

displayModification3 = theSession.DisplayManager.NewDisplayModification()
displayModification3.NewLayer = featureLayer

for each temp as DisplayableObject in theSession.Parts.Work.Sketches
sketchToMove.add(temp)
displayModification2.Apply(sketchToMove.ToArray)
sketchToMove.Clear
sketchLayer = sketchLayer + 1
displayModification2.NewLayer = sketchLayer
next

for each temp1 as DisplayableObject in theSession.Parts.Work.Features
try
featureToMove.add(temp1)
displayModification3.Apply(featureToMove.ToArray)
catch ex as exception

end try

next

displayModification1.Dispose()

End Sub

End Module

if instead I just try to run

displayModification2.Apply(theSession.Parts.Work.Features.ToArray)

then I get the error Value of type '1-dimensional array of NXOpen.Features.Feature' cannot be converted to '1-dimensional array of NXOpen.DisplayableObject' because 'NXOpen.Features.Feature' is not derived from 'NXOpen.DisplayableObject'.

Seriously thanks for your patience and all of your help with this.

An NXOpen.Feature object does not inherit from the DisplayableObject class, so the features cannot be treated as if they were displayable objects. Most (all?) features create some sort of object in the graphics window; the feature's .GetEntities method will return those objects. The .GetEntities method of a datum csys feature will return 3 datum planes, 3 datum axes, a coordinate system, and a point. These objects can be moved to a new layer, the feature itself cannot.

Thank you so this works it's some code you've used to help someone in the past that I modified for Csys. Would I need to define every feature I want to move this way? There's no efficient way to move all Feature.Features in bulk to one layer?


for each temp1 as Features.Feature in theSession.Parts.Work.Features
if TypeOf(temp1) is Features.DatumCsys then
dim myCsys as Features.DatumCsys = temp1
for each featureitem as DisplayableObject in myCsys.GetEntities
featureToMove.add(featureitem)
next
end if
next

That's one way to deal with moving the objects created by a datum csys feature. I wouldn't focus too much attention on the .Features collection; for moving objects to layers, it is more trouble than it is worth. Features, as you've found out, are not displayable objects where you can directly change the color or layer. I think of feature objects as invisible interpreters that sit between NX and the user. At its heart, NX creates geometry: vertices, curves, faces, bodies, etc. As a user, you want an easy way to create and edit geometry. The feature object takes the input from the user and instructs NX how to create/edit the geometry. The feature object manages some graphical object, but is distinctly different from that graphical object.

For example, a Datum Plane feature (NXOpen.Features.DatumPlaneFeature) creates a Datum Plane object; it has a .DatumPlane property that gives you direct access to the Datum Plane (NXOpen.DatumPlane) object that it manages. Similarly, the DatumPlane object has a .Feature property that gives you access to the feature that manages it.

Many features create faces or modify existing ones. In this case, it doesn't make sense to use .GetEntities and move those to a new layer. Faces are forever attached to a Body object and cannot be moved to a new layer independent of the body. Where the body goes, the faces (and edges) follow.

Instead of writing code to account for each and every type of feature, it will be easier to start with the collections that inherit from DisplayableObject and work from there (for example: .Bodies, .Curves, .Sketches, .Datums, etc). There are still some caveats to code for; you would not want to blindly move all datum axes to one layer and all datum planes to another - that would lead to some strange behavior with datum csys objects...