标签:
一般情况下,我们自己搭建模型的功力还不够,大多都是在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文件,导出模型后就只有.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()之类的一系列函数。当然导出的模型也可以继续导入。
标签:
原文地址:http://www.cnblogs.com/theblueberry/p/4228085.html