简体   繁体   English

C#与WPF中的ModelVisual3D对象进行交互

[英]C# Interacting with ModelVisual3D objects in WPF

Hey I am using a WPF 3D scene and successfully loaded a few .stl models into it. 嘿,我正在使用WPF 3D场景并成功将一些.stl模型加载到其中。 I am basicall using a method to select and deselect these objects, based on where my mouse in the windows is: 我基本上都使用一种方法来选择和取消选择这些对象,具体取决于我的鼠标在Windows中的位置:

 private void UIElement_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            var viewport = (HelixViewport3D)sender;
            var firstHit = viewport.Viewport.FindHits(e.GetPosition(viewport)).FirstOrDefault();
            if (firstHit != null)
            {
                this.viewModel.Select(firstHit.Visual);

            }
            else
            {
                this.viewModel.Select(null);

            }
        }

So I have the selected ModelVisual3D and could store it. 因此,我选择了ModelVisual3D并将其存储。 However here is the main problem: 但是,这是主要问题:

My models are generated based on certain data and an associated .stl model. 我的模型是基于某些数据和关联的.stl模型生成的。 Basicall I import the .stl model and show it in my 3D scene, but the problem is I have no idea how connect my other data to the model. Basicall我导入.stl模型并在3D场景中显示它,但是问题是我不知道如何将其他数据连接到模型。

For example when I select the visual, I want to show another windows with information like: material, dimensions, company. 例如,当我选择视觉效果时,我想显示另一个窗口,其中包含诸如材料,尺寸,公司之类的信息。

But I can't figure out how to determine, which unique ModelVisual3D object is selected at the moment. 但是我不知道如何确定当前选择了哪个唯一的ModelVisual3D对象。 There seem to be no properties I could use to my advantage doing something like: 似乎没有可以利用的属性,例如:

ModelImporter tr = new ModelImporter();
            var model = tr.Load("C:\\Users\\...\\Pictures\\a.stl");
            ModelVisual3D test = new ModelVisual3D();

            test.Content = model;

//Here I would like to save the id of my visual model to identify and
//associate it with my other data later

            int myUniqueModelID=test.Properties.UNIQUEID

What about using a dictionary to keep the mapping between your data reference and ModelVisual3D reference. 如何使用字典来保持数据引用和ModelVisual3D引用之间的映射。

Like 喜欢

    Dictionary<ModelVisual3D, StlDataObject> modelDataMap = new Dictionary<ModelVisual3D, StlDataObject>();
    public void LoadModelWithData(string dataFilePath, string stlModelPath)
    {
        ModelImporter tr = new ModelImporter();
        var model = tr.Load("C:\\Users\\...\\Pictures\\a.stl");
        ModelVisual3D test = new ModelVisual3D();

        test.Content = model;

        //Load the datafile from file (or pass it this method)
        StlDataObject IdForOurStlModel = GetStlDataFromFile(dataFilePath);
        modelDataMap.Add(test, IdForOurStlModel);
    }

Then when you need to find your data from the hit test in your viewport, you just check if this dictionary contains a key for the pressed ModelVisual3d and return the value related to that key if it does. 然后,当您需要在视口中的命中测试中查找数据时,只需检查此字典是否包含用于按下的ModelVisual3d的键,然后返回与该键相关的值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM