Exemplo n.º 1
0
        private void treeViewContainer_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            IfdConcept ifdConcept = e.Node.Tag as IfdConcept;

            if (ifdConcept != null && e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Tag == null)
            {
                // load child concepts
                try
                {
                    if (!this.backgroundWorkerConcepts.IsBusy)
                    {
                        this.backgroundWorkerConcepts.RunWorkerAsync(e.Node);
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
                catch
                {
                    e.Cancel = true;
                }
            }
        }
Exemplo n.º 2
0
        public static void Download(DocProject project, System.ComponentModel.BackgroundWorker worker, string baseurl, string username, string password)
        {
            if (project.Sections[4].Schemas.Count == 0)
            {
                project.Sections[4].Schemas.Add(new DocSchema());
            }

            string url = baseurl + "api/4.0/session/login?email=" + HttpUtility.UrlEncode(username) + "&password="******"POST";
            request.ContentLength = 0;
            request.ContentType   = "application/x-www-form-urlencoded";
            request.Accept        = "application/json";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            System.IO.Stream       stream = response.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(stream);
            string body = reader.ReadToEnd();

            string cookie = response.Headers.Get("Set-Cookie");

            string[] parts     = cookie.Split(new char[] { ';', ',' }); // bug? comma separates session ID
            string   match     = "peregrineapisessionid=";
            string   sessionid = null;

            foreach (string part in parts)
            {
                if (part.StartsWith(match))
                {
                    sessionid = part.Substring(match.Length);
                    break;
                }
            }

            /*-Get all users:
             * var client = new RestClient("http://test.bsdd.buildingsmart.org/api/4.0/IfdUser/");
             * var request = new RestRequest(Method.GET);
             * request.AddHeader("cookie", "peregrineapisessionid=thesessionid");
             * request.AddHeader("accept", "application/json");
             * request.AddHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
             * IRestResponse response = client.Execute(request);*/

            /*- Get all languages:
             * var client = new RestClient("http://test.bsdd.buildingsmart.org/api/4.0/IfdLanguage/");
             * var request = new RestRequest(Method.GET);
             * request.AddHeader("cookie", "peregrineapisessionid={{sessionId}}");
             * request.AddHeader("accept", "application/json");
             * request.AddHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
             * IRestResponse response = client.Execute(request);*/
            request               = (HttpWebRequest)HttpWebRequest.Create(baseurl + "api/4.0/IfdLanguage/");
            request.Method        = "GET";
            request.ContentLength = 0;
            request.ContentType   = "application/x-www-form-urlencoded; charset=UTF-8";
            request.Headers.Add("cookie", "peregrineapisessionid=" + sessionid);
            request.Accept = "application/json";
            response       = (HttpWebResponse)request.GetResponse();

            stream = response.GetResponseStream();
            reader = new System.IO.StreamReader(stream);
            body   = reader.ReadToEnd();

            body.ToString();

            request               = (HttpWebRequest)HttpWebRequest.Create(baseurl + "api/4.0/IfdConcept/search/filter/language/1ASQw0qJqHuO00025QrE$V/type/NEST/Pset_*");
            request.Method        = "GET";
            request.ContentLength = 0;
            request.ContentType   = "application/x-www-form-urlencoded; charset=UTF-8";
            request.Headers.Add("cookie", "peregrineapisessionid=" + sessionid);
            request.Accept = "application/json";
            response       = (HttpWebResponse)request.GetResponse();

            stream = response.GetResponseStream();
            reader = new System.IO.StreamReader(stream);
            body   = reader.ReadToEnd();

            System.IO.Stream       ms     = new System.IO.MemoryStream();
            System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
            writer.Write(body);
            writer.Flush();
            ms.Position = 0;
            //System.IO.MemoryStream mstream = new System.IO.MemoryStream()

            ResponseSearch ifdRoot;

            try
            {
                DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
                //settings.UseSimpleDictionaryFormat = true;
                System.Runtime.Serialization.Json.DataContractJsonSerializer ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(ResponseSearch));
                ifdRoot = (ResponseSearch)ser.ReadObject(ms);
            }
            catch
            {
                return;
            }

            // PERF: consider API on server that would return all information at once (currently 150+ round trips)
            for (int iConc = 0; iConc < ifdRoot.IfdConcept.Length; iConc++)
            {
                IfdConcept concept = ifdRoot.IfdConcept[iConc];
                worker.ReportProgress((int)(100.0 * (double)iConc / (double)ifdRoot.IfdConcept.Length));

                // api/4.0/IfdPSet/{guid}/ifcVersion/{ifcVersion}/XML

#if true                                   //figure out version info // language code starting at 5th character then lowercase -- e.g. "IFC-2X4" -> "2x4"
                string ifcversion = "2x4"; // "IFC4";// "ifc-2X4"; //???? what should this be ???? -- code "ifc-2X4" appears in headers; "IFC-2x4" appears in UI; "IFC4" is official schema name
                request               = (HttpWebRequest)HttpWebRequest.Create(baseurl + "api/4.0/IfdPSet/" + concept.guid + "/ifcVersion/" + ifcversion + "/XML");
                request.Method        = "GET";
                request.ContentLength = 0;
                request.ContentType   = "application/x-www-form-urlencoded; charset=UTF-8";
                request.Headers.Add("cookie", "peregrineapisessionid=" + sessionid);
                request.Accept = "application/xml";
#endif
#if false // worked April 1, but no longer works as of 2017-06-13 (http body returns "null" -- as in 4 character string) -- issue with test server -- CoBuilder merge wiped out content??
                request               = (HttpWebRequest)HttpWebRequest.Create(baseurl + "api/4.0/IfdConcept/" + concept.guid + "/children");
                request.Method        = "GET";
                request.ContentLength = 0;
                request.ContentType   = "application/x-www-form-urlencoded; charset=UTF-8";
                request.Headers.Add("cookie", "peregrineapisessionid=" + sessionid);
                request.Accept = "application/json";
                //request.Accept = "application/xml";
#endif
                try
                {
                    response = (HttpWebResponse)request.GetResponse();

                    stream = response.GetResponseStream();


                    reader = new System.IO.StreamReader(stream);
                    body   = reader.ReadToEnd();
                    if (body != null && body != "null") // !!!!
                    {
                        ms     = new MemoryStream();
                        writer = new StreamWriter(ms, Encoding.Unicode);
                        writer.Write(body);
                        writer.Flush();
                        ms.Position = 0;

                        try
                        {
                            using (FormatXML format = new FormatXML(ms, typeof(PropertySetDef), null, null))
                            {
                                format.Load();
                                PropertySetDef psd = (PropertySetDef)format.Instance;
                                Program.ImportPsd(psd, project);
                            }
                        }
                        catch
                        {
                            System.Diagnostics.Debug.WriteLine("Error downloading property set: " + concept.guid.ToString());
                        }


#if false
                        ResponsePset ifdResponse;
                        DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
                        //settings.UseSimpleDictionaryFormat = true;
                        System.Runtime.Serialization.Json.DataContractJsonSerializer ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(ResponsePset));

                        try
                        {
                            System.IO.Stream       xs      = new System.IO.MemoryStream();
                            System.IO.StreamWriter xwriter = new System.IO.StreamWriter(xs);
                            xwriter.Write(body);
                            xwriter.Flush();
                            xs.Position = 0;

                            ifdResponse = (ResponsePset)ser.ReadObject(xs);

                            ifdResponse.ToString();


                            DocPropertySet docPset = new DocPropertySet();
                            docPset.Uuid = new Guid();// concept.guid;
                            foreach (IfdName ifdName in concept.fullNames)
                            {
                                // if english
                                if (ifdName.languageFamily == "IFC")
                                {
                                    docPset.Name = ifdName.name;
                                }
                                else
                                {
                                    DocLocalization docLoc = new DocLocalization();
                                    docLoc.Locale = ifdName.language.languageCode;
                                    docLoc.Name   = ifdName.name;
                                    docPset.Localization.Add(docLoc);
                                }
                            }
                            docPset.Documentation = concept.definitions.description;
                            project.Sections[4].Schemas[0].PropertySets.Add(docPset);

                            foreach (IfdConceptInRelationship ifdProp in ifdResponse.IfdConceptInRelationship)
                            {
                                //ifdProp.fullNames[0].
                                DocProperty docProp = new DocProperty();
                                if (ifdProp.definitions != null)
                                {
                                    docProp.Documentation = ifdProp.definitions.description;
                                }
                                docPset.Properties.Add(docProp);

                                foreach (IfdName ifdName in ifdProp.fullNames)
                                {
                                    // if english
                                    if (ifdName.languageFamily == "IFC")
                                    {
                                        //docProp.Name = ifdName.name;
                                        string[] nameparts = ifdName.name.Split('.');
                                        if (nameparts.Length == 2)
                                        {
                                            docPset.Name = nameparts[0];
                                            docProp.Name = nameparts[1];
                                        }
                                    }
                                    else
                                    {
                                        DocLocalization docLoc = new DocLocalization();
                                        docLoc.Locale = ifdName.language.languageCode;
                                        docLoc.Name   = ifdName.name;
                                        docProp.Localization.Add(docLoc);
                                    }
                                }
                            }
                        }
#endif
                    }
                }
                catch
                {
                    //...
                    return;
                }
            }
        }