Sample Teamcenter .NET C# program : QueryItemRevisionClassificationObjects

Sample Teamcenter .NET C# program : QueryItemRevisionClassificationObjects

Siemens PLMS provides programming examples for illustration only, and assumes that you are familiar
with the programming language being demonstrated and the tools used to create and debug
programs.  Siemens PLMS Support Professionals can help explain the functionality of a particular
interface, but we will not modify these examples to provide additional functionality or rewrite the interface
to meet your specific needs.



/* 
This is a SOA C# client example to show how to get Classification (Icm0) object from 'ItemRevision.IMAN_classification' property and 
then call Classification.GetClassificationObjects(...) to get the classificaiotn property and print them.

The C# HelloTeamcenter example was used to test this code (availabe in the soa_client.zip). The the class as follows: 

    var c = new Classification();

    c.GetClassificationObjectsFromItemRevisions();
    c.PrintClassificationData();
    
*/

using System;
using System.Collections.Generic;
using Teamcenter.Services.Strong.Query;

using Teamcenter.Services.Strong.Core;
using Teamcenter.Services.Strong.Classification;
using Teamcenter.Soa.Client.Model;
using Teamcenter.Soa.Client.Model.Strong;
using Teamcenter.Soa.Common;

using Finder = Teamcenter.Services.Strong.Query._2007_06.Finder;
using Classification_2007_01 = Teamcenter.Services.Strong.Classification._2007_01.Classification;


namespace Teamcenter.Code.code
{
    class Classification
    {
        private readonly FinderService fs;
        private readonly ClassificationService cs;
        private List<ModelObject> objList = new List<ModelObject>();
        public Classification()
        {
            fs = FinderService.getService(Teamcenter.ClientX.Session.getConnection());
            cs = ClassificationService.getService(Teamcenter.ClientX.Session.getConnection());

            SetObjectPolicy();
        }

        public void GetClassificationObjectsFromItemRevisions()
        {
            var find = new Finder.WSOFindCriteria
            {
                ObjectType = "Mfg0MENCToolRevision", // Update with your ItemRevision type. 
                Scope = "WSO_scope_All"
            };
            var findSet = new Finder.WSOFindSet
            {
                Criterias = new[] { find }
            };

            var resp = fs.FindWorkspaceObjects(new[] { findSet });

            if(!ServiceDataError(resp.ServiceData))
            {
                foreach (var output in resp.OutputList)
                {
                    foreach (var wobj in output.FoundObjects)
                    {
                        if (wobj is ItemRevision)
                        {
                            var itemRev = wobj as ItemRevision;

                            foreach(var mobj in itemRev.IMAN_classification)
                            {
                                if (mobj is Icm0)
                                    objList.Add(mobj);
                            }
                        }
                    }
                }
            }
        }

        public void PrintClassificationData()
        {
            if (objList.Count > 0)
            {
                var resp = cs.GetClassificationObjects(objList.ToArray());

                if (!ServiceDataError(resp.Data))
                {
                    foreach (var value in resp.ClsObjs.Values)
                    {
                        PrintClassificationObject(value as Classification_2007_01.ClassificationObject);
                        Console.WriteLine();
                    }
                }
            }
        }

        protected void PrintClassificationObject(Classification_2007_01.ClassificationObject classObj)
        {
            Console.Write("CID: " + classObj.ClassId);
            Console.Write(", ID: " + classObj.InstanceId);
            Console.WriteLine(", Unit: " + classObj.UnitBase);

            foreach(var prop in classObj.Properties)
            {
                foreach(var value in prop.Values)
                {
                    Console.Write("{");
                    Console.Write(value.DbValue);
                    Console.Write(", ");
                    Console.Write(value.DisplayValue);
                    Console.Write("}, ");
                }
            }
            Console.WriteLine();
        }

        protected bool ServiceDataError(ServiceData data)
        {
            if (data.sizeOfPartialErrors() > 0)
            {
                for (int i = 0; i < data.sizeOfPartialErrors(); i++)
                {
                    foreach (string msg in data.GetPartialError(i).Messages)
                        Console.WriteLine(msg);
                }

                return true;
            }

            return false;
        }

        protected void SetObjectPolicy()
        {
            var session = SessionService.getService(ClientX.Session.getConnection());
            var policy = new ObjectPropertyPolicy();

            policy.AddType(new PolicyType("ItemRevision", new string[] { "IMAN_classification" }));
 
            session.SetObjectPropertyPolicy(policy);
        }
    }
}
posted @   张永全-PLM顾问  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示