Beispiel #1
0
        private string CreateNamespaceFromConcept(IfdConcept ifdConcept)
        {
            string suffix = GetConceptIdentifier(ifdConcept);
            string prefix = null;
            IList <IfdConceptInRelationship> listHost = GetRelatedConcepts(ifdConcept.guid, false);

            foreach (IfdConceptInRelationship rel in listHost)
            {
                IfdRelationshipTypeEnum reltype;
                if (Enum.TryParse <IfdRelationshipTypeEnum>(rel.relationshipType, out reltype))
                {
                    switch (reltype)
                    {
                    case IfdRelationshipTypeEnum.GROUPS:
                        prefix = CreateNamespaceFromConcept(rel);
                        break;
                    }
                }
            }

            if (prefix != null)
            {
                return(prefix + "." + suffix);
            }
            else
            {
                return(suffix);
            }
        }
Beispiel #2
0
        private string GetConceptDescription(IfdConcept ifdConcept)
        {
            foreach (IfdDescription ifdName in ifdConcept.definitions)
            {
                if (ifdName.language.languageCode == "en")
                {
                    return(ifdName.description);
                }
            }

            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Finds an existing item by IFC identifier
        /// </summary>
        /// <param name="baseurl"></param>
        /// <param name="sessionid"></param>
        /// <param name="identifier"></param>
        /// <returns></returns>
        private IfdConcept SearchConcept(string identifier, IfdConceptTypeEnum type)
        {
            try
            {
                string         url     = this.m_uri + "api/4.0/IfdConcept/search/filter/language/" + LanguageID + "/type/" + type.ToString() + "/" + identifier;
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method        = "GET";
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = 0;
                request.Accept        = "application/json";
                request.Headers.Add("cookie", "peregrineapisessionid=" + this.m_session);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream          stream   = response.GetResponseStream();

                DataContractJsonSerializer ser    = new DataContractJsonSerializer(typeof(ResponseSearch));
                ResponseSearch             search = (ResponseSearch)ser.ReadObject(stream);
                if (search != null && search.IfdConcept != null && search.IfdConcept.Length > 0)
                {
                    // return latest one
                    string     lastversiondate = null;
                    IfdConcept thatconc        = null;
                    foreach (IfdConcept conc in search.IfdConcept)
                    {
                        if (lastversiondate == null)
                        {
                            lastversiondate = conc.versionDate;
                            thatconc        = conc;
                        }
                        else
                        {
                            if (String.Compare(conc.versionDate, lastversiondate) > 0)
                            {
                                lastversiondate = conc.versionDate;
                                thatconc        = conc;
                            }
                        }
                    }

                    return(thatconc);

                    //return search.IfdConcept[search.IfdConcept.Length - 1]; // use last one
                }
            }
            catch (Exception xx)
            {
                System.Diagnostics.Debug.WriteLine(xx.Message);
            }

            return(null);
        }
Beispiel #4
0
        private string GetConceptName(IfdConcept ifdConcept)
        {
            if (ifdConcept.fullNames != null)
            {
                foreach (IfdName ifdName in ifdConcept.fullNames)
                {
                    if (ifdName.language.languageCode == "en")
                    {
                        return(ifdName.name);
                    }
                }
            }

            return(null);
        }
Beispiel #5
0
        /// <summary>
        /// Returns the IFC identifier (e.g. entity name) for a concept.
        /// </summary>
        /// <param name="ifdConcept"></param>
        /// <returns></returns>
        private string GetConceptIdentifier(IfdConcept ifdConcept)
        {
            if (ifdConcept.fullNames != null)
            {
                foreach (IfdName ifdName in ifdConcept.fullNames)
                {
                    if (ifdName.languageFamily == "IFC")
                    {
                        return(ifdName.name);
                    }
                }
            }

            return(null);
        }
Beispiel #6
0
        /// <summary>
        /// Reads a data type from the dictionary and all properties within.
        /// </summary>
        /// <param name="identifier">Identifier of the object (IFC class name).</param>
        /// <returns></returns>
        public Type ReadType(string identifier)
        {
            // first try for entity concept
            IfdConcept conc = SearchConcept(identifier, IfdConceptTypeEnum.SUBJECT);

            if (conc == null)
            {
                // then try for measure
                conc = SearchConcept(identifier, IfdConceptTypeEnum.MEASURE);
                if (conc == null)
                {
                    return(null);
                }
            }

            Type type = CreateTypeFromConcept(conc);

            return(type);
        }
Beispiel #7
0
        /// <summary>
        /// Reads items within namespace
        /// </summary>
        /// <param name="identifier"></param>
        /// <returns>List of namespaces (System.String) and/or types (System.Type) within namespace.</returns>
        public Dictionary <string, object> ReadNamespace(string identifier)
        {
            // first try for entity concept
            IfdConcept conc = SearchConcept(identifier, IfdConceptTypeEnum.BAG);

            if (conc == null)
            {
                return(null);
            }

            IList <IfdConceptInRelationship> list = GetRelatedConcepts(conc.guid, true);
            Dictionary <string, object>      ls   = new Dictionary <string, object>();

            foreach (IfdConceptInRelationship cr in list)
            {
                IfdConceptTypeEnum conctype;
                if (Enum.TryParse <IfdConceptTypeEnum>(cr.conceptType, out conctype))
                {
                    string name = GetConceptName(cr);

                    switch (conctype)
                    {
                    case IfdConceptTypeEnum.SUBJECT:
                    {
                        Type t = CreateTypeFromConcept(cr);
                        ls.Add(cr.guid, t);
                    }
                    break;

                    case IfdConceptTypeEnum.BAG:
                        ls.Add(cr.guid, name);
                        break;
                    }
                }
            }

            return(ls);
        }
Beispiel #8
0
        /// <summary>
        /// Writes a data type and all public instance properties.
        /// Such type may correspond to an IFC entity or a ConceptRoot of a model view definition (which may also correspond to a property set)
        /// The type is nested according to namespace tokens.
        /// The type is linked to superclass according to base type.
        /// The type is expanded into subclasses if the last property is an enumeration for a classification (e.g. PredefinedType).
        /// Any referenced types are also retrieved and written if they don't yet exist.
        /// Localized names and descriptions are extracted from .NET resources.
        /// </summary>
        /// <param name="type"></param>
        public void WriteType(Type type)
        {
            if (type == null)
            {
                return;
            }

            if (m_mapTypes.ContainsKey(type))
            {
                return; // already written
            }
            string name = type.Name;
            DisplayNameAttribute attrName = (DisplayNameAttribute)type.GetCustomAttribute <DisplayNameAttribute>();

            if (attrName != null)
            {
                name = attrName.DisplayName;
            }

            string desc = null;
            DescriptionAttribute attrDesc = (DescriptionAttribute)type.GetCustomAttribute <DescriptionAttribute>();

            if (attrDesc != null)
            {
                desc = attrDesc.Description;
            }

            IfdConceptTypeEnum      conctype = IfdConceptTypeEnum.SUBJECT;
            IfdRelationshipTypeEnum relbase  = IfdRelationshipTypeEnum.SPECIALIZES;

            if (type.Name.StartsWith("Pset") || type.Name.StartsWith("Qto"))
            {
                // hack
                conctype = IfdConceptTypeEnum.BAG;
                relbase  = IfdRelationshipTypeEnum.ASSIGNS_COLLECTIONS;
            }
            else if (type.IsValueType || type.IsEnum)
            {
                conctype = IfdConceptTypeEnum.MEASURE;
            }

            // retrieve existing -- enable once final uploaded correctly!
#if false
            IfdConcept conc = SearchConcept(type.Name, conctype);
            if (conc != null)
            {
                this.m_mapTypes.Add(type, conc.guid);
                return;
            }
#endif

            DisplayAttribute[] localize = (DisplayAttribute[])type.GetCustomAttributes(typeof(DisplayAttribute), false);
            IfdBase            ifdThis  = CreateConcept(type.Name, name, desc, conctype, localize);
            if (ifdThis == null)
            {
                return;
            }

            this.m_mapTypes.Add(type, ifdThis.guid);

            // get namespace
            string[] namespaces       = type.Namespace.Split('.');
            string   guidSchemaParent = null;
            string   guidSchemaChild  = null;
            for (int iNS = 0; iNS < namespaces.Length; iNS++)
            {
                string ns = namespaces[iNS];
                if (!this.m_mapNamespaces.TryGetValue(ns, out guidSchemaChild))
                {
                    StringBuilder sbQual = new StringBuilder();
                    for (int x = 0; x <= iNS; x++)
                    {
                        if (x > 0)
                        {
                            sbQual.Append(".");
                        }
                        sbQual.Append(namespaces[x]);
                    }
                    string qualname = sbQual.ToString();

                    // call server to find namespace
                    IfdConcept ifdNamespace = SearchConcept(qualname, IfdConceptTypeEnum.BAG);
                    if (ifdNamespace != null)
                    {
                        guidSchemaChild = ifdNamespace.guid;
                    }
                    else
                    {
                        IfdBase ifdNS = CreateConcept(qualname, ns, String.Empty, IfdConceptTypeEnum.BAG, null);
                        guidSchemaChild = ifdNS.guid;
                    }

                    this.m_mapNamespaces.Add(ns, guidSchemaChild);

                    if (guidSchemaParent != null)
                    {
                        CreateRelationship(guidSchemaParent, guidSchemaChild, IfdRelationshipTypeEnum.COLLECTS);
                    }
                }

                if (iNS == namespaces.Length - 1)
                {
                    CreateRelationship(guidSchemaChild, ifdThis.guid, IfdRelationshipTypeEnum.COLLECTS);
                }

                guidSchemaParent = guidSchemaChild;
            }

            // get base type
            if (type.IsClass && type.BaseType != typeof(object))
            {
                WriteType(type.BaseType);

                string guidbase = null;
                if (m_mapTypes.TryGetValue(type.BaseType, out guidbase))
                {
                    CreateRelationship(guidbase, ifdThis.guid, relbase);
                }
            }

            //PropertyInfo[] props = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
            //foreach (PropertyInfo prop in props)
            FieldInfo[] fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
            foreach (FieldInfo prop in fields)
            {
                // write the property itself

                // resolve property type
                Type typeProp = prop.FieldType;
                if (typeProp.IsGenericType && typeProp.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    typeProp = typeProp.GetGenericArguments()[0];
                }

                if (typeProp.IsGenericType && typeProp.GetGenericTypeDefinition() == typeof(IList <>))
                {
                    typeProp = typeProp.GetGenericArguments()[0];
                }

                if (typeProp.IsGenericType && typeProp.GetGenericTypeDefinition() == typeof(ISet <>))
                {
                    typeProp = typeProp.GetGenericArguments()[0];
                }

                if (typeProp.IsArray)
                {
                    typeProp = typeProp.GetElementType();
                }

                // special case for specialization
                if (prop.Name.Equals("_PredefinedType"))
                {
                    FieldInfo[] enumvals = typeProp.GetFields(BindingFlags.Public | BindingFlags.Static);
                    foreach (FieldInfo f in enumvals)
                    {
                        if (f.Name != "USERDEFINED" && f.Name != "NOTDEFINED")
                        {
                            string fname = f.Name;
                            DisplayNameAttribute attrFName = (DisplayNameAttribute)f.GetCustomAttribute <DisplayNameAttribute>();
                            if (attrName != null)
                            {
                                fname = attrName.DisplayName;
                            }

                            string fdesc = null;
                            DescriptionAttribute attrFDesc = (DescriptionAttribute)f.GetCustomAttribute <DescriptionAttribute>();
                            if (attrDesc != null)
                            {
                                fdesc = attrDesc.Description;
                            }

                            DisplayAttribute[] localizePredef = (DisplayAttribute[])f.GetCustomAttributes(typeof(DisplayAttribute), false);
                            IfdBase            ifdPredef      = CreateConcept(type.Name + "." + f.Name, fname, fdesc, IfdConceptTypeEnum.SUBJECT, localizePredef);
                            if (ifdPredef != null)
                            {
                                CreateRelationship(ifdThis.guid, ifdPredef.guid, IfdRelationshipTypeEnum.SPECIALIZES);
                            }
                        }
                    }
                }
                else if (conctype == IfdConceptTypeEnum.BAG)// psetprop.FieldType.IsValueType) //!!!
                {
                    name     = type.Name;
                    attrName = (DisplayNameAttribute)prop.GetCustomAttribute <DisplayNameAttribute>();
                    if (attrName != null)
                    {
                        name = attrName.DisplayName;
                    }

                    desc     = null;
                    attrDesc = (DescriptionAttribute)prop.GetCustomAttribute <DescriptionAttribute>();
                    if (attrDesc != null)
                    {
                        desc = attrDesc.Description;
                    }

                    string  propidentifier = type.Name + "." + prop.Name.Substring(1);
                    IfdBase ifdProp        = CreateConcept(propidentifier, name, desc, IfdConceptTypeEnum.PROPERTY, null);
                    if (ifdProp == null)
                    {
                        return;
                    }

                    // include the property
                    CreateRelationship(ifdThis.guid, ifdProp.guid, IfdRelationshipTypeEnum.COLLECTS);

                    WriteType(typeProp);

                    if (typeProp.IsValueType)
                    {
                        string guidDataType = null;
                        if (m_mapTypes.TryGetValue(typeProp, out guidDataType))
                        {
                            CreateRelationship(ifdProp.guid, guidDataType, IfdRelationshipTypeEnum.ASSIGNS_MEASURES);//...verify
                        }
                    }
                }
            }
        }
Beispiel #9
0
        private Type CreateTypeFromConcept(IfdConcept ifdConcept)
        {
            Type type = null;

            if (m_mapDynamicTypes.TryGetValue(ifdConcept.guid, out type))
            {
                return(type);
            }

            // get base definition if any
            Type   typeBase = null;
            string ns       = null;
            IList <IfdConceptInRelationship> listHost = GetRelatedConcepts(ifdConcept.guid, false);

            foreach (IfdConceptInRelationship rel in listHost)
            {
                IfdRelationshipTypeEnum reltype;
                if (Enum.TryParse <IfdRelationshipTypeEnum>(rel.relationshipType, out reltype))
                {
                    switch (reltype)
                    {
                    case IfdRelationshipTypeEnum.SPECIALIZES:     // base type
                        typeBase = CreateTypeFromConcept(rel);
                        break;

                    case IfdRelationshipTypeEnum.GROUPS:     // namespace
                        ns = CreateNamespaceFromConcept(rel);
                        break;
                    }
                    //rel.guid
                }
            }

            // construct a type
            TypeAttributes attr = TypeAttributes.Public | TypeAttributes.Class;
            // abstract...

            string typename = GetConceptIdentifier(ifdConcept);

            try
            {
                TypeBuilder tb = this.m_module.DefineType(ns + "." + typename, attr, typeBase);

                // custom attributes, e.g. guid
                if (ifdConcept.guid != null)
                {
                    IfcGloballyUniqueId ifcGuid = IfcGloballyUniqueId.Parse(ifdConcept.guid);
                    Guid guid = ifcGuid.ToGuid();

                    ConstructorInfo        conReq = typeof(GuidAttribute).GetConstructor(new Type[] { typeof(string) });
                    CustomAttributeBuilder cabReq = new CustomAttributeBuilder(conReq, new object[] { guid.ToString() });
                    tb.SetCustomAttribute(cabReq);
                }

                string displayname = GetConceptName(ifdConcept);
                if (displayname != null)
                {
                    ConstructorInfo        conReq = typeof(DisplayNameAttribute).GetConstructor(new Type[] { typeof(string) });
                    CustomAttributeBuilder cabReq = new CustomAttributeBuilder(conReq, new object[] { displayname });
                    tb.SetCustomAttribute(cabReq);
                }

                string description = GetConceptDescription(ifdConcept);
                if (description != null)
                {
                    ConstructorInfo        conReq = typeof(DescriptionAttribute).GetConstructor(new Type[] { typeof(string) });
                    CustomAttributeBuilder cabReq = new CustomAttributeBuilder(conReq, new object[] { description });
                    tb.SetCustomAttribute(cabReq);
                }

                // properties
                IList <IfdConceptInRelationship> listItem = GetRelatedConcepts(ifdConcept.guid, true);
                foreach (IfdConceptInRelationship rel in listItem)
                {
                    IfdRelationshipTypeEnum reltype;
                    if (Enum.TryParse <IfdRelationshipTypeEnum>(rel.relationshipType, out reltype))
                    {
                        switch (reltype)
                        {
                        case IfdRelationshipTypeEnum.ASSIGNS_PROPERTIES:     //...
                            break;
                        }
                    }
                }

                type = tb.CreateType();
                this.m_mapDynamicTypes.Add(ifdConcept.guid, type);
                return(type);
            }
            catch
            {
                return(null); // duplicate??
            }
        }
Beispiel #10
0
 private PropertyInfo CreatePropertyInfoFromConcept(IfdConcept ifdConcept)
 {
     //...
     return(null);
 }