Ejemplo n.º 1
0
        /// <summary>
        /// PUT JSON document data into
        /// the specified mongoDB collection.
        /// </summary>
        public static bool Put(
            string collection_name_and_id,
            InstanceData data,
            out string result)
        {
            var client = new RestClient(RestApiBaseUrl);

            var request = new RestRequest(_api_version + "/"
                                          + collection_name_and_id, Method.PUT);

            request.RequestFormat = DataFormat.Json;

            // Check what we actually send.
            //Debug.Print( request.JsonSerializer.Serialize( data ) );

            request.AddBody(data); // uses JsonSerializer

            // Didn't work for me
            //request.AddObject( data ); // http://matthewschrager.com/2013/02/19/restsharp-post-body/

            // POST params instead of body is more efficient
            // since there's no serialization to JSON.
            // But our data is a bit large for that.
            //request.AddParameter("A", "foo");
            //request.AddParameter("B", "bar");

            IRestResponse response = client.Execute(request);

            bool rc = System.Net.HttpStatusCode.Accepted
                      == response.StatusCode;

            if (rc)
            {
                result = response.Content; // raw content as string
            }
            else
            {
                //if( response.ErrorMessage.Equals(
                //  "Unable to connect to the remote server" ) )
                //  "does the database exist at all?"

                result = response.ErrorMessage;

                Debug.Print("HTTP PUT error: " + result);
            }
            return(rc);
        }
Ejemplo n.º 2
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            Application   app   = uiapp.Application;
            Document      doc   = uiapp.ActiveUIDocument.Document;

            if (string.IsNullOrEmpty(doc.PathName))
            {
                message = "Please store RVT file "
                          + "before launching this command";

                return(Result.Failed);
            }

            // Retrieve LMV credentials.

            string client_key;
            string client_secret;

            if (!Util.GetLvmCredentials(
                    out client_key, out client_secret))
            {
                message = _key_secret_prompt;
                return(Result.Failed);
            }

            // Upload and translate model file.
            // Todo: add linked files as well.

            ArrayList fileList = new ArrayList();
            //fileList.Add( asm.FullFileName );
            //fileList.Add( doc.PathName );

            // Revit blocks the current file.
            // Copy it to a location with no spaces in the name.
            // From then on, the whole upload process could be spawned off into a separate thread.
            // Well, no, we do need the URN.

            string filename = Path.Combine(
                Path.GetTempPath(),
                Path.GetFileName(doc.PathName));

            filename.Replace(' ', '_');
            Debug.Print(filename);
            File.Delete(filename);
            File.Copy(doc.PathName, filename, true);
            fileList.Add(filename);

            //foreach( Document oRefDoc in asm.AllReferencedDocuments )
            //{
            //  fileList.Add( oRefDoc.FullDocumentName );
            //}

            MultiFileUploader.Util u = new MultiFileUploader.Util(
                "https://developer.api.autodesk.com");

            string urn = u.UploadFilesWithReferences(
                client_key, client_secret, "comphoundrvtbucket",
                fileList, null, true);

            if (string.IsNullOrEmpty(urn))
            {
                message = "View and Data API upload and "
                          + "translation failed.";

                return(Result.Failed);
            }

            Transform projectLocationTransform
                = Util.GetProjectLocationTransform(doc);

            // Loop through all family instance elements
            // and export their data.

            FilteredElementCollector instances
                = new FilteredElementCollector(doc)
                  .OfClass(typeof(FamilyInstance));

            Result rc = Result.Succeeded;

            InstanceData instanceData;

            int n = 0;

            foreach (FamilyInstance e in instances)
            {
                Debug.Print(e.Id.IntegerValue.ToString());

                instanceData = new InstanceData(e,
                                                projectLocationTransform, urn);

                if (!Util.Put("instances/" + e.UniqueId,
                              instanceData, out message))
                {
                    rc = Result.Failed;
                    break;
                }
                ++n;
            }
            Debug.Print(
                "CompHound uploaded {0} instance{1}.",
                n, (1 == n ? "" : "s"));

            return(rc);
        }