AnyCAD应用——修改指定点,线,面的坐标实现物体移动

发布时间 2023-07-01 13:23:54作者: 季饶风风

课题需求: 三维模型位置随实物位置改变而动态改变的功能,通过顶点位置坐标的改变,修改模型在显示中的位置。


功能验证:目前,可通过选中顶点手动输入坐标的方式,模拟验证此功能。


首先,点击“文件”工具栏,选择导入实体,导入一个step文件,这是一种常用的三维实体模型格式。

 

void OnOpenModel()
{
var dlg = new Microsoft.Win32.OpenFileDialog
{
DefaultExt = ".stp",
Filter = "Models (*.igs;*.iges;*.stp;*.step;*.brep;*.stl)|*.igs;*.iges;*.stp;*.step;*.brep;*.stl"
};
if (dlg.ShowDialog() != true)
return;

SceneNode? node = null;
ProgressView pv = new ProgressView(() =>
{
var shape = ShapeIO.Open(dlg.FileName);
if (shape == null)
return;
node = BrepSceneNode.Create(shape, null, null, 0, false);
});
pv.ShowDialog();

if (node == null)
return;

mRenderView.ShowSceneNode(node);
mRenderView.ZoomAll();
}


然后,在导入的三维模型中选中某一点,即可在“操作对象”属性栏中看到该点在世界坐标系中的实际位置。

 

var item = itr.Current();
msg += $"\nNodeId: {item.GetNodeId()}";
msg += $"\nUserId: {item.GetUserId()}";
msg += $"\nShapeId: {item.GetShapeIndex()}";
msg += $"\nPrimitiveId: {item.GetPrimitiveIndex()}";
msg += $"\nType: {item.GetShapeType().ToString()}";
msg += $"\nTopoShapeId: {item.GetTopoShapeId().ToString()}";
var pt = item.GetPosition();
msg += $"\nPosition: {pt.x} {pt.y} {pt.z}";


如果需要修改该点的位置,可以点击“自定义位置调整”工具栏,在“目标位置”属性栏中输入目标坐标,或者使用鼠标拖动该点,即可将目标顶点移动到所需位置。并且,您可通过点击软件右上角位置方块,观察三维模型各个位置的动态变化。

 选择顶点

 ViewStateGuard.SetPickFilter(EnumShapeFilter.VertexEdgeFace);

 对应的XAML代码

<Expander Header="操作对象" IsExpanded="True">
<StackPanel Margin="7,7,7,0" Orientation="Vertical">
<StackPanel Orientation="Horizontal" Margin="0,0,0,7">
<au:AuStepButton Content="选择对象" Margin="0,0,7,0"
IsChecked ="{Binding SourceObjectChecked, Mode=OneWay}"
IsStateFocused="{Binding SourceObjectFocused, Mode=OneWay}"
StepName ="SourceObject"
Click="OnClickStep"/>
<au:AuImageButton Click="OnChoosePoint"
IsStateFocused="{Binding SourcePointFocused, Mode=OneWay}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,0,7">
<au:AuNumericBox Width="70" Value="{Binding SourcePointX}" Margin="0,0,7,0"/>
<au:AuNumericBox Width="70" Value="{Binding SourcePointY}" Margin="0,0,7,0"/>
<au:AuNumericBox Width="70" Value="{Binding SourcePointZ}" Margin="0,0,7,0"/>
</StackPanel>
</StackPanel>
</Expander>
<Expander Header="目标位置" IsExpanded="True">
<StackPanel Orientation="Horizontal" Margin="7,7,0,7">
<au:AuNumericBox Width="70" Value="{Binding TargetPointX}" Margin="0,0,7,0"/>
<au:AuNumericBox Width="70" Value="{Binding TargetPointY}" Margin="0,0,7,0"/>
<au:AuNumericBox Width="70" Value="{Binding TargetPointZ}" Margin="0,0,7,0"/>
</StackPanel>
</Expander>

对应的坐标变量

GPnt _SourcePoint = new GPnt();
[ExpandXYZ]
public GPnt SourcePoint
{
get => _SourcePoint;
set
{
_SourcePoint.SetXYZ(value.XYZ());
OnPropertyChanged(nameof(SourcePoint));
OnPropertyChanged(nameof(SourcePointX));
OnPropertyChanged(nameof(SourcePointY));
OnPropertyChanged(nameof(SourcePointZ));
}
}

GPnt _TargetPoint = new GPnt();
[ExpandXYZ]
public GPnt TargetPoint
{
get => _TargetPoint;
set
{
_TargetPoint.SetXYZ(value.XYZ());
OnPropertyChanged(nameof(TargetPoint));
OnPropertyChanged(nameof(TargetPointX));
OnPropertyChanged(nameof(TargetPointY));
OnPropertyChanged(nameof(TargetPointZ));

UpdateObject();
}
}

应用变换

void UpdateObject()
{
if (_SourceObject == null)
{
return;
}

var vec = new GVec(_SourcePoint, _TargetPoint);
var trf = Matrix4.makeTranslation(Vector3.From(vec));

_SourceObject.SetTransform(trf);
_SourceObject.RequestUpdate();

ViewContext.RequestUpdate(EnumUpdateFlags.Scene);
}


此功能仅改变三维模型的三维坐标,不改变模型本身的形状。