How to run snippet of code

How to run snippet of code

Iev60047
Advocate Advocate
410 Views
3 Replies
Message 1 of 4

How to run snippet of code

Iev60047
Advocate
Advocate

Complete beginner here in writing code for Revit. I am trying to run the following bit of code.

 

drafting.CropBoxActive = true;
drafting.CropBoxVisible = true;

private static void ExtendViewCrop(View drafting, Group detail)
        {
            BoundingBoxXYZ crop = (drafting!= null ? drafting.CropBox : null);
            if (crop == null || crop.Max == null || crop.Min == null || crop.Transform == null || detail == null)
                return;
            BoundingBoxXYZ detailBox = detail.get_BoundingBox(drafting);
            BoundingBoxXYZ extendedCrop = new BoundingBoxXYZ();
            extendedCrop.Transform = crop.Transform;

            if (detailBox == null || detailBox.Max == null || detailBox.Min == null)
                return;

            extendedCrop.Max = detailBox.Max + (extendedCrop.Transform.BasisX + extendedCrop.Transform.BasisY / 2) * 0.03;
            extendedCrop.Min = detailBox.Min + (-extendedCrop.Transform.BasisX + -extendedCrop.Transform.BasisY / 2) * 0.03;
            drafting.CropBox = extendedCrop;
        }

 

In general when I see something like this, do I try and copy and paste the code to a macro within Revit (i.e. using SharpDevelop)? Or should I attempt to create a project in VisualStudio (i.e. building a .cs file and adding a .addin file). This seems like something so obvious it's not stated anywhere. I followed the tutorial here

 

I tried building the following code in a project: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Architecture;

namespace ExportImage
{
    public class Class1
    {
        drafting.CropBoxActive = true;
        drafting.CropBoxVisible = true;

        private static void ExtendViewCrop(View drafting, Group detail)
        {
            BoundingBoxXYZ crop = (drafting != null ? drafting.CropBox : null);
            if (crop == null || crop.Max == null || crop.Min == null || crop.Transform == null || detail == null)
                return;
            BoundingBoxXYZ detailBox = detail.get_BoundingBox(drafting);
            BoundingBoxXYZ extendedCrop = new BoundingBoxXYZ();
            extendedCrop.Transform = crop.Transform;

            if (detailBox == null || detailBox.Max == null || detailBox.Min == null)
                return;

            extendedCrop.Max = detailBox.Max + (extendedCrop.Transform.BasisX + extendedCrop.Transform.BasisY / 2) * 0.03;
            extendedCrop.Min = detailBox.Min + (-extendedCrop.Transform.BasisX + -extendedCrop.Transform.BasisY / 2) * 0.03;
            drafting.CropBox = extendedCrop;
        }
    }
}

But only ended up with plenty of errors when I tried to build. When someone else runs the above (first) snippet of code, how exactly do they get it to run? Thanks so much. I am continuing to work through tutorials.

0 Likes
Accepted solutions (1)
411 Views
3 Replies
Replies (3)
Message 2 of 4

EATREVITPOOPCAD
Collaborator
Collaborator
Accepted solution

C# you have to compile in Visual Studio. There is a steep learning curve to doing C# compared to other alternatives that access Revit API using Python in pyRevit or Dynamo. Buuuut if you go this route I personally believe it is much more rewarding in the long run. So if you have a little bit of patience, forget the code you are trying to run here at the moment, and do this tutorial from beginning to finish.  It is a pretty sweet tutorial, and I found it to be perfect for someone like me who didn't know what he is doing at all (I have a Fine Arts degree 😄 ) After the tutorial: find problems you want to solve and come back here for help, you will get plenty of it.

 

What I didn't know at the time of going the C# route, was that there are easier paths to take such as pyRevit, and you can achieve probably the same exact thing you want to do with C#. But I believe learning C# has many advantages also. (plus Python is easy to learn after C#) Overall I am very happy I stuck with C# and if could go back I would double down LOL.  At end of day depends what you want to do.  

 

Something to keep in mind, pyRevit has a lot more of newbie friendly documentation too, here are some fun ones I have on my to watch list (pyRevit is still on my to learn list) 

https://www.youtube.com/watch?v=YhL_iOKH-1M

https://www.youtube.com/watch?v=JkNMGk_aXsg

The definition of insanity is doing the same thing over and over again and expecting different results
Message 3 of 4

Iev60047
Advocate
Advocate

Thank you @EATREVITPOOPCAD for your thoughtful and kind answer! I was surprisingly able to write some code today (have minimal background in C+, R and Python, so some of the coding things kind of carry over). and yes, I figured out that you need to create (at least to the best of my understanding) a class in VisualStudio (I still can't figure out the SharpDevelop platform, a lot jankier in my opinion than VisualStudio) and define what references you are using and the main class. Below is code that produced an image for me.

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Architecture;

//We will not be committing any changes to the model, so read only for the transaction mode.
[Transaction(TransactionMode.ReadOnly)]
public class Main : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        //Get application and document objects
        UIApplication uiapp = commandData.Application;
        Document doc = uiapp.ActiveUIDocument.Document;
     

        //
        ImageExportOptions imgExportOpts = new ImageExportOptions();
        {
            imgExportOpts.ZoomType = ZoomFitType.FitToPage;
            imgExportOpts.PixelSize = 500;
            imgExportOpts.FilePath = "C:/Users/username/Documents/Test.TIFF";
            imgExportOpts.FitDirection = FitDirectionType.Vertical;
            imgExportOpts.HLRandWFViewsFileType = ImageFileType.TIFF;
            imgExportOpts.ShadowViewsFileType = ImageFileType.TIFF;
            imgExportOpts.ImageResolution = ImageResolution.DPI_72;
            imgExportOpts.ShouldCreateWebSite = false;
        };

        doc.ExportImage(imgExportOpts);

        //Returns success if programs runs well
        return Result.Succeeded;
    }
     
}

 

 

Edit: For other complete beginners such as myself, this file and the other ".addin" were all that were needed to get this program to run. For understanding what the ".addin" file is and how to make one go to this webpage for the tutorial. 

 

Edit 2: Here is the text file I created for the ".addin" file. P.S. I replaced my username with "username" for privacy's sake.

 

<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
 <AddIn Type="Command">
       <Name>ExportImage</Name>
       <FullClassName>Main</FullClassName>
       <Text>ExportImage</Text>
       <Description>Exports an image from current view.</Description>
       <VisibilityMode>AlwaysVisible</VisibilityMode>
       <Assembly>C:\Users\username\source\repos\ExportImage\ExportImage\bin\Debug\ExportImage.dll</Assembly>
       <AddInId>604b1052-F742-4951-8576-C261D1993107</AddInId>
    <VendorId>ADSK</VendorId>
    <VendorDescription>Autodesk, Inc, www.autodesk.com</VendorDescription>
 </AddIn>
</RevitAddIns>

 

Message 4 of 4

EATREVITPOOPCAD
Collaborator
Collaborator

You’re welcome and you got this!

The definition of insanity is doing the same thing over and over again and expecting different results
0 Likes