I have tried to reproduce the problem and was not able to do that.
I used the following xaml (as simple as possible):
Code:
<Grid Name="RootGrid">
<Viewport3D Name="MainViewport" />
<cameras:TargetPositionCamera Name="Camera1"
TargetPosition="0 0 0"
Heading="30" Attitude="-20" Bank="0" />
</Grid>
and the following code behind:
Code:
this.Loaded += delegate (object sender, RoutedEventArgs args)
{
var boxVisual3D = new Ab3d.Visuals.BoxVisual3D()
{
CenterPosition = new Point3D(0, 0, 0),
Size = new Size3D(100, 40, 80),
Material = new DiffuseMaterial(Brushes.Green)
};
MainViewport.Children.Add(boxVisual3D);
Camera1.FitIntoView();
};
This did not throw any exception.
I have also tried to replace the BoxVisual3D with wireframe object and still it worked.
I have also checked the code for FitIntoView and did not find a possible cause of NullReferenceException there.
Could you please post the full stack trace for the exception (you can also send it by email).
Note:
The simple code above does not work well - because the TargetViewport3D property is not set in the TargetPositionCamera, the Camera is not correctly initialized at the time of the FitIntoView call - therefore the camera is not adjusted correctly. To fix that I advice to set the TargetViewport3D:
Code:
<cameras:TargetPositionCamera Name="Camera1"
TargetPosition="0 0 0"
Heading="30" Attitude="-20" Bank="0"
TargetViewport3D="{Binding ElementName=MainViewport}"/>
It is also possible to call Camera1.Refresh() method before FitIntoView - this will search the visual tree and automatically find the Viewport3D (without setting TargetViewport3D). Because this will be done before the FitIntoView call, the FitIntoView will correctly adjust the camera. I will improve that in the next version.