Revit QRCode Generator

QR Codes were first created to help in the manufacturing process. In 1994 Denso Wave, a subsidiary of Toyota Motor Corporation, created a Lean Manufacturing Process which is a systematic method for the elimination of waste within a manufacturing system.

So by Definition QR Codes (Quick Response Code) are LEAN and quite versatile, they are able to store many different types of data like Contact information, URLs, Email Address, Geo Locations or just plain text. Using them for retrieving the information is quite easy and inexpensive too. 

In the recent years more and more construction companies are utilizing that technology by embedding the QR Codes into blueprints or printing them into labels. This expands the amount of information available for the field beyond the constrains of a printed piece of paper.

For this blog post we are going to utilize the power of a cloud base API for the QR Code generation  and System.Net to retrieve that generated image from the cloud.

In this example we are going to generate a QR code image with an embedded link that points to the material specs website page using the URL link that is located inside this family’s Type Properties.

typeproperties

First we need to be able to select the family or families  and add them into a list.

            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;
            
            List<ElementId> ids = new List<ElementId>();
            Selection sel = uidoc.Selection;

            ICollection<ElementId> selIds = sel.GetElementIds();

            if (0 < selIds.Count)
            {
                foreach (ElementId id in selIds)
                {
                    ids.Add(id);
                }
            }

            if (0 == selIds.Count)
            {
                IList<Reference> refs = null;

                try
                {
                    refs = sel.PickObjects(ObjectType.Element,
                    "Please Element(s) to Generate QrCode.");
                }
                catch (Autodesk.Revit.Exceptions
                .OperationCanceledException)
                {
                    //  return Result.Cancelled;
                }
                ids = new List<ElementId>(
                refs.Select<Reference, ElementId>(
                r => r.ElementId));
            }

Then from that list of Element ids  we need to get their Url parameter data inside the Type Properties.

typeproperties1

 foreach (ElementId id in ids)
            {
                Element el = doc.GetElement(id);
                FamilyInstance elsym = el as FamilyInstance;
                FamilySymbol famsym = elsym.Symbol as FamilySymbol;
                string data = Uri.EscapeUriString(famsym.get_Parameter(BuiltInParameter.ALL_MODEL_URL).AsString());
            }

 

Now that we have the QR Code data, need a place to save the generated image and its name ( we are going to use the family instance name).

 string folderpath = @"C:\Your Folder Path\";
 string Filename = el.Name;

Then we are going to create a method that uses System.Net to establish the communication with the cloud QR Code generator, and downloads the generated image into our computer.

 private static void GenerateQRCode(string Data,string folderpath, string fileName)
        {
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://api.qrserver.com/v1/create-qr-code/?size=900x900&data=" + Data);
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                if ((response.StatusCode == System.Net.HttpStatusCode.OK ||
                    response.StatusCode == System.Net.HttpStatusCode.Moved ||
                    response.StatusCode == System.Net.HttpStatusCode.Redirect) &&
                    response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
                {
                    using (System.IO.Stream inputStream = response.GetResponseStream())
                    using (System.IO.Stream outputStream = System.IO.File.OpenWrite(folderpath + fileName + ".png"))
                    {
                        byte[] buffer = new byte[4096];
                        int bytesRead;
                        do
                        {
                            bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                            outputStream.Write(buffer, 0, bytesRead);
                        } while (bytesRead != 0);
                    }
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Error", ex.Message);
            }

        }

Now we just call the method inside the “foreach” statement.

GenerateQRCode(data, folderpath, Filename);
foreach (ElementId id in ids)
{
Element el = doc.GetElement(id);
FamilyInstance elsym = el as FamilyInstance;
FamilySymbol famsym = elsym.Symbol as FamilySymbol;
string data = Uri.EscapeUriString(famsym.get_Parameter(BuiltInParameter.ALL_MODEL_URL).AsString());
string folderpath = @"C:\Your Folder Path\";
string Filename = el.Name;

GenerateQRCode(data, folderpath, Filename);
}

Now if we run the macro we should have our QR Code image. It can be used in a Label Schedule, Title block or inside a detail sheet.


ScheduleQrcode

 

Screen shot of the scanned QR Code using my Phone.

qrc

Here is the Complete code for this example and video.

 

        public void QrCodeGenerator()
        {
            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;
            
            List<ElementId> ids = new List<ElementId>();
            Selection sel = uidoc.Selection;

            ICollection<ElementId> selIds = sel.GetElementIds();

            if (0 < selIds.Count)
            {
                foreach (ElementId id in selIds)
                {
                    ids.Add(id);
                }
            }

            if (0 == selIds.Count)
            {
                IList<Reference> refs = null;

                try
                {
                    refs = sel.PickObjects(ObjectType.Element,
                    "Please Element(s) to Generate QrCode.");
                }
                catch (Autodesk.Revit.Exceptions
                .OperationCanceledException)
                {
                    //  return Result.Cancelled;
                }
                ids = new List<ElementId>(
                refs.Select<Reference, ElementId>(
                r => r.ElementId));
            }

            foreach (ElementId id in ids)
            {
                Element el = doc.GetElement(id);
                FamilyInstance elsym = el as FamilyInstance;
                FamilySymbol famsym = elsym.Symbol as FamilySymbol;
                string data = Uri.EscapeUriString(famsym.get_Parameter(BuiltInParameter.ALL_MODEL_URL).AsString());
                string folderpath = @"C:\Your Folder Path\";
                string Filename = el.Name;

                GenerateQRCode(data, folderpath, Filename);
            }
        }

        private static void GenerateQRCode(string Data, string folderpath, string fileName)
        {
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://api.qrserver.com/v1/create-qr-code/?size=900x900&data=" + Data);
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                if ((response.StatusCode == System.Net.HttpStatusCode.OK ||
                    response.StatusCode == System.Net.HttpStatusCode.Moved ||
                    response.StatusCode == System.Net.HttpStatusCode.Redirect) &&
                    response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
                {
                    using (System.IO.Stream inputStream = response.GetResponseStream())
                    using (System.IO.Stream outputStream = System.IO.File.OpenWrite(folderpath + fileName + ".png"))
                    {
                        byte[] buffer = new byte[4096];
                        int bytesRead;
                        do
                        {
                            bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                            outputStream.Write(buffer, 0, bytesRead);
                        } while (bytesRead != 0);
                    }
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Error", ex.Message);
            }
        }
Spread the knowledge

1 thought on “Revit QRCode Generator”

Comments are closed.