using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.EditorInput; using Teamcenter.Hello; using Teamcenter.Soa.Client; using Teamcenter.ClientX; using Teamcenter.Services.Strong.Core; using Teamcenter.Soa.Client.Model; using Teamcenter.Soa.Exceptions; using Teamcenter.Services.Strong.Query; using Teamcenter.Services.Strong.Core._2007_06.DataManagement; using Teamcenter.Services.Strong.Core._2006_03.Reservation; using Teamcenter.Services.Strong.Core._2006_03.FileManagement; using Teamcenter.Soa.Internal.Client.Model; using Teamcenter.Soa.Internal.Client; using HelloTeamcenter.hello; using User = Teamcenter.Soa.Client.Model.Strong.User; using Folder = Teamcenter.Soa.Client.Model.Strong.Folder; using WorkspaceObject = Teamcenter.Soa.Client.Model.Strong.WorkspaceObject; using Item = Teamcenter.Soa.Client.Model.Strong.Item; using ItemRevision = Teamcenter.Soa.Client.Model.Strong.ItemRevision; using ImanQuery = Teamcenter.Soa.Client.Model.Strong.ImanQuery; using DataSet = Teamcenter.Soa.Client.Model.Strong.Dataset; using SavedQueryResults = Teamcenter.Services.Strong.Query._2007_06.SavedQuery.SavedQueryResults; using ImanFile = Teamcenter.Soa.Client.Model.Strong.ImanFile; using BaseClassInput = Teamcenter.Services.Strong.Core._2007_06.DataManagement.BaseClassInput; //using Teamcenter.Services.Strong.Businessmodeler._2007_06. namespace HelloTeamcenter.form { public partial class Search : Form { public Search() { InitializeComponent(); } public List folderlist = new List(); public List itemlist = new List(); public List itemvisionlist = new List(); public List datasetlist = new List(); public Document appdoc; public User user; public Editor ed; public bool hasRight = true; public string[] enableTypes; private void getTypes(string rootType) { BaseClassInput[] input =new BaseClassInput[1]; input[0] = new BaseClassInput(); input[0].BaseClass = rootType; } private void Search_Load(object sender, EventArgs e) { ed = appdoc.Editor; //comboBox2 DataManagementService dmService = DataManagementService.getService(Session.getConnection()); this.comboBox2.Items.Clear(); Tool tool = new Tool(); SavedQueryResults found = tool.getSearchUser(); if (found != null && found.NumOfObjects > 0) { for (int i = 0; i < found.NumOfObjects; i++) { User userobj = found.Objects[i] as User; ModelObject[] objects = { userobj }; String[] attributes = { "User ID", "Name" }; dmService.RefreshObjects(objects); dmService.GetProperties(objects, attributes); string userid = userobj.User_id; string username = userobj.User_name; string tempname = userid + "(" + username + ")"; if (this.comboBox2.Items.Contains(tempname)) continue; else this.comboBox2.Items.Add(tempname); } } else ed.WriteMessage("没有查找到用户的信息。\n"); this.textBox1.Text = ""; this.textBox2.Text = ""; } //查找 private void button1_Click(object sender, EventArgs e) { string name = textBox1.Text; string item_id = textBox2.Text; string type = comboBox1.Text; //ed.WriteMessage("选择类型:"+ type +"\n"); string owner = comboBox2.Text; //ed.WriteMessage("选择用户:" + owner + "\n"); if (name == "") { name = "*"; } if (item_id == "") { item_id = "*"; } if (type == "") { type = "*"; } if (owner == "") { owner = "*"; } else { int pos = owner.IndexOf('('); owner = owner.Substring(0, pos); ed.WriteMessage("用户" + owner + "\n"); } DataManagementService dmService = DataManagementService.getService(Session.getConnection()); Tool tool = new Tool(); SavedQueryResults found = tool.getSearchItem(item_id, name, type, owner); if (found != null && found.NumOfObjects > 0) { this.treeView1.Nodes.Clear(); for (int i = 0; i < found.NumOfObjects; i++) { Item item = found.Objects[i] as Item; ModelObject[] objects = { item }; String[] attributes = { "object_string"}; dmService.RefreshObjects(objects); dmService.GetProperties(objects, attributes); string inname = item.Object_string; TreeNode childnode = new TreeNode(inname, 3, 3); this.treeView1.Nodes.Add(childnode); ALLOBJECT perobject = new ALLOBJECT(); perobject.workobject = item; perobject.treenode = childnode; itemlist.Add(perobject); } foreach (TreeNode ch in treeView1.Nodes) { ch.EnsureVisible(); } } } private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { TreeNode nownode = this.treeView1.SelectedNode; getChild(nownode); foreach (TreeNode ch in nownode.Nodes) { ch.EnsureVisible(); } } private void getChild(TreeNode nownode) { nownode.Nodes.Clear(); DataManagementService dmService = DataManagementService.getService(Session.getConnection()); WorkspaceObject[] contents = null; string nodetext = nownode.Text; int imageindex = nownode.SelectedImageIndex; if (imageindex == 3) { foreach (ALLOBJECT perobject in itemlist) { if (perobject.treenode.Equals(nownode)) { Item item = perobject.workobject as Item; ModelObject[] itemrevisionlist = null; ModelObject[] objects = { item }; String[] attributes = { "revision_list" }; dmService.RefreshObjects(objects); dmService.GetProperties(objects, attributes); itemrevisionlist = item.Revision_list; for (int i = 0; i < itemrevisionlist.Length; i++) { ItemRevision itemrevision = itemrevisionlist[i] as ItemRevision; ModelObject[] objects1 = { itemrevision }; String[] attributes1 = { "object_string", "object_type" }; dmService.RefreshObjects(objects1); dmService.GetProperties(objects1, attributes1); string name = itemrevision.Object_string; string type = itemrevision.Object_type; ALLOBJECT inperobject = new ALLOBJECT(); inperobject.workobject = itemrevision; TreeNode childnode = new TreeNode(name, 4, 4); inperobject.treenode = childnode; nownode.Nodes.Add(childnode); itemvisionlist.Add(inperobject); } break; } } } else if (imageindex == 4) { foreach (ALLOBJECT perobject in itemvisionlist) { if (perobject.treenode.Equals(nownode)) { ItemRevision itemrevision = perobject.workobject as ItemRevision; ExpandGRMRelationsPref myPref = new ExpandGRMRelationsPref(); RelationAndTypesFilter2 myFilter = new RelationAndTypesFilter2(); myFilter.RelationName = "IMAN_specification"; //String[] typeVec = { "D5DWG", "Folder" }; myFilter.ObjectTypeNames = enableTypes; myPref.ExpItemRev = false; RelationAndTypesFilter2[] myfilter = { myFilter }; myPref.Info = myfilter; ModelObject[] objects1 = { itemrevision }; ExpandGRMRelationsResponse myResp = dmService.ExpandGRMRelationsForPrimary(objects1, myPref); ExpandGRMRelationsOutput[] myoutput = myResp.Output; for (int i = 0; i < myoutput.Length; i++) { ExpandGRMRelationsOutput one_out = myoutput[i]; for (int j = 0; j < one_out.OtherSideObjData.Length; j++) { ExpandGRMRelationsData otherSideData = one_out.OtherSideObjData[j]; for (int k = 0; k < otherSideData.OtherSideObjects.Length; k++) { Type typeinfo = otherSideData.OtherSideObjects[k].GetType(); string typename = typeinfo.Name; if (typename == "Folder") { Folder infold = otherSideData.OtherSideObjects[k] as Folder; ModelObject[] objects = { infold }; String[] attributes = { "object_string", "object_type" }; dmService.RefreshObjects(objects); dmService.GetProperties(objects, attributes); string name = infold.Object_string; string type = infold.Object_type; ALLOBJECT inperobject = new ALLOBJECT(); inperobject.workobject = infold; TreeNode childnode = new TreeNode(name, 1, 1); inperobject.treenode = childnode; nownode.Nodes.Add(childnode); folderlist.Add(inperobject); } else { DataSet dateset = otherSideData.OtherSideObjects[k] as DataSet; ModelObject[] objects = { dateset }; String[] attributes = { "object_string" }; dmService.RefreshObjects(objects); dmService.GetProperties(objects, attributes); string name = dateset.Object_string; ALLOBJECT inperobject = new ALLOBJECT(); inperobject.workobject = dateset; inperobject.name = name; TreeNode childnode = new TreeNode(name, 0, 0); inperobject.treenode = childnode; nownode.Nodes.Add(childnode); datasetlist.Add(inperobject); } } } } break; } } } else if (imageindex == 1) { foreach (ALLOBJECT perobject in folderlist) { if (perobject.treenode.Equals(nownode)) { Folder fl = perobject.workobject as Folder; ModelObject[] objects = { fl }; String[] attributes = { "contents" }; dmService.RefreshObjects(objects); dmService.GetProperties(objects, attributes); contents = fl.Contents; for (int i = 0; i < contents.Length; i++) { WorkspaceObject childobj = contents[i]; ModelObject[] objects1 = { childobj }; String[] attributes1 = { "object_string", "object_type" }; dmService.RefreshObjects(objects1); dmService.GetProperties(objects1, attributes1); string name = childobj.Object_string; string type = childobj.Object_type; if (childobj is Folder) { ALLOBJECT inperobject = new ALLOBJECT(); inperobject.workobject = childobj; Folder infl = childobj as Folder; if (type == "Newstuff Folder") { TreeNode childnode = new TreeNode(name, 6, 6); inperobject.treenode = childnode; nownode.Nodes.Add(childnode); } else if (type == "Mail Folder") { TreeNode childnode = new TreeNode(name, 5, 5); inperobject.treenode = childnode; nownode.Nodes.Add(childnode); } else{ TreeNode childnode = new TreeNode(name, 1, 1); inperobject.treenode = childnode; nownode.Nodes.Add(childnode); } folderlist.Add(inperobject); } else if (childobj is Item) { ALLOBJECT inperobject = new ALLOBJECT(); inperobject.workobject = childobj; Item item = childobj as Item; TreeNode childnode = new TreeNode(name, 3, 3); inperobject.treenode = childnode; nownode.Nodes.Add(childnode); itemlist.Add(inperobject); } } break; } } } } //打开 private void button2_Click(object sender, EventArgs e) { DataManagementService dmService = DataManagementService.getService(Session.getConnection()); Reservation res = ReservationService.getService(Session.getConnection()); TreeNode nownode = this.treeView1.SelectedNode; if (nownode.SelectedImageIndex == 0) { foreach (ALLOBJECT perobject in this.datasetlist) { if (perobject.treenode.Equals(nownode)) { DialogResult isopen = MessageBox.Show("您是否确定打开该图纸?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (isopen == DialogResult.None || isopen == DialogResult.No) { return; } DataSet mydateset = perobject.workobject as DataSet; ModelObject[] objects = { mydateset }; String[] attributes = { "is_modifiable", "checked_out", "ref_list" }; dmService.RefreshObjects(objects); dmService.GetProperties(objects, attributes); if (mydateset.Is_modifiable == false || (mydateset.Checked_out == "Y")) { DialogResult moresult = MessageBox.Show("您没有修改权限或文件已经签出,是否以只读方式打开?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (moresult == DialogResult.Yes) { hasRight = false; } else { return; } } else if (mydateset.Is_modifiable) { DialogResult moresult = MessageBox.Show("是否签出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (moresult == DialogResult.Yes) { //加锁安全机制,同步 ModelObject[] dataobj = { mydateset }; res.Checkout(dataobj, "", ""); ed.WriteMessage("文件已签出\n"); } } ModelObject[] dsfilevec = mydateset.Ref_list; //ed.WriteMessage("长度:" + dsfilevec.Length); //ed.WriteMessage(dsfilevec[0].GetType().ToString()); ImanFile dsfile = dsfilevec[0] as ImanFile; ModelObject[] objects1 = { dsfile }; String[] attributes1 = { "relative_directory_path", "original_file_name" }; dmService.RefreshObjects(objects1); dmService.GetProperties(objects1, attributes1); //ed.WriteMessage("路径:" + dsfile.Relative_directory_path + "\n"); //ed.WriteMessage("文件名:" + dsfile.Original_file_name + "\n"); string newfilename = dsfile.Original_file_name; string tempdir = System.Environment.GetEnvironmentVariable("TEMP").ToString(); //ed.WriteMessage("TEMP:" + tempdir.ToString() + "\n"); FileManagementService fmService = FileManagementService.getService(Teamcenter.ClientX.Session.getConnection()); ImanFile[] objects2 = { dsfile }; FileTicketsResponse tickets = fmService.GetFileReadTickets(objects2); //ed.WriteMessage("tickets : " + tickets.Tickets.Count + "\n"); //foreach (System.Collections.DictionaryEntry ticket in tickets.Tickets) //{ // ed.WriteMessage("键:" + ticket.Key + "\n" + "值:" + ticket.Value + "\n"); //} Teamcenter.Soa.Client.FileManagementUtility fmu = new Teamcenter.Soa.Client.FileManagementUtility(Teamcenter.ClientX.Session.getConnection()); Teamcenter.Soa.Client.GetFileResponse getFileResponse = fmu.GetFiles(dsfilevec); FileInfo[] fileinfovec = getFileResponse.GetFiles(); //ed.WriteMessage("文件个数:" + fileinfovec.Length + "\n"); FileInfo file = fileinfovec[0]; string newtempfile = tempdir + "\\" + newfilename; System.IO.File.Copy(file.FullName, newtempfile, true); System.IO.File.SetAttributes(newtempfile, FileAttributes.Normal); DocumentCollection acdocmgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager; if (File.Exists(newtempfile)) { acdocmgr.Open(newtempfile,false); //Object pdata = newtempfile; //acdocmgr.ExecuteInApplicationContext(c_back, pdata); //acdocmgr.Add(newtempfile); } else { MessageBox.Show("对不起,用户临时目录下不存在此文件\n"); } this.Hide(); this.Dispose(); break; } } } else { //ed.WriteMessage("请选择正确的数据集类型\n"); MessageBox.Show("请选择正确的数据集类型"); return; } } //private void c_back(Object data) //{ // DocumentCollection acdocmgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager; // if (acdocmgr.IsApplicationContext) // { // acdocmgr.Open(Convert.ToString(data)); // } //} } }