Eyeshot Ultimate 学习笔记(2)

导入模型

一般情况下,我们自己搭建模型的功力还不够,大多都是在3Dmax中做好模型,导出成模型文件,然后再导入Eyeshot视图中。导入的代码包括:

OpenFileDialog openFileDialog1 = new OpenFileDialog();
string theFilter = "Points|*.asc|" + "Stereolithography|*.stl|" + "WaveFront OBJ|*.obj";
theFilter += "|IGES|*.igs; *.iges|" + "STEP|*.stp; *.step";
openFileDialog1.Filter = theFilter;
openFileDialog1.Multiselect = false;
openFileDialog1.AddExtension = true;
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
   viewportLayout1.Entities.Clear();
   ReadFileAsynch rfa = null;
   switch (openFileDialog1.FilterIndex)
   {
      case 1:
      rfa = new ReadASC(openFileDialog1.FileName);
      break;
      case 2:
      rfa = new ReadSTL(openFileDialog1.FileName);
      break;
      case 3:
      rfa = new ReadOBJ(openFileDialog1.FileName);
      break;
      case 4:
      rfa = new ReadIGES(openFileDialog1.FileName);
      break;
      case 5:
      rfa = new ReadSTEP(openFileDialog1.FileName);
      break;
   }
   viewportLayout1.StartWork(rfa);
}

这里还没有完,因为导入模型这个过程是异步导入,所以还有另外一个函数:

         private void viewportLayout1_WorkCompleted(object sender, WorkCompletedEventArgs e)
        {
            if (e.WorkUnit is ReadFileAsynch)//导入文件
            {
                ReadFileAsynch rfa = (ReadFileAsynch)e.WorkUnit;
                Entity[] entList = rfa.Entities;
                entityListClone = (Entity[])entList.Clone();
                rfa.AddToScene(viewportLayout1);

                viewportLayout1.SetView(viewType.Top);
                viewportLayout1.ZoomFit();
            }
        }

这段代码表示如果执行的work是导入文件,那么将导入的模型呈现在视图中,并且设置视图角度以及模型的相对大小。

如果该模型包含mtl材料文件和图片格式的纹理文件,那么导入就需要用到另外一种方式,这里仍然拿OBJ格式的模型为例子:

用函数ReadOBJ(Stream objStream, Stream materialStream, Dictionary<string, Stream> texturesStream, meshEdgeStyleType edgeStyle)

FileStream fs1 = new FileStream(@"..\..\Models\demo.obj", FileMode.Open); //obj
FileStream fs2 = new FileStream(@"..\..\Models\demo.mtl", FileMode.Open); //mtl
FileStream fs3 = new FileStream(@"..\..\Models\demo.jpg", FileMode.Open); //texture
Dictionary<string, Stream> texturesDic = new Dictionary<string, Stream>();
texturesDic.Add("demo.jpg", fs3);
ReadOBJ ro = new ReadOBJ(fs1, fs2, texturesDic, meshEdgeStyleType.Sharp);
viewportLayout1.StartWork(ro); 

viewportLayout1_WorkCompleted函数中的部分不用改变。

导出模型

导出模型的文件只有一个,比如导入模型时有一个.mtl文件和一个.obj文件,导出模型后就只有.obj文件了。

导出模型的代码如下:

                SaveFileDialog sfd = new SaveFileDialog();
                string theFilter = "OBJ|*.OBJ";
                theFilter += "|IGES|*.igs; *.iges|" + "STEP|*.stp; *.step";
                sfd.Filter = theFilter;
                string savePath = fileName;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    if (!string.IsNullOrEmpty(sfd.FileName))
                    {
                        savePath = sfd.FileName;
                    }
                    string extention = savePath.Substring(savePath.LastIndexOf('.') + 1).ToLower();
                    switch (extention)
                    {
                        case "obj":
                            viewportLayout1.WriteOBJ(savePath, false);
                            break;
                        case "igs":
                            viewportLayout1.WriteIGES(savePath, false);
                            break;
                        case "iges":
                            viewportLayout1.WriteIGES(savePath, false);
                            break;
                        case "stp":
                            viewportLayout1.WriteSTEP(savePath, false);
                            break;
                        case "step":
                            viewportLayout1.WriteSTEP(savePath, false);
                            break;
                    }
                }

导出主要的函数为WriteOBJ(),WriteIGES()之类的一系列函数。当然导出的模型也可以继续导入。

posted @ 2015-01-16 11:12  雏菊之秋  阅读(1574)  评论(0编辑  收藏  举报