From the course: Revit: Creating C# Plugins

Element locations

From the course: Revit: Creating C# Plugins

Start my 1-month free trial

Element locations

- We have learned so far that different elements can be, created using either a point, for a point based element, such as a piece of furniture, or curves, for line based elements such as a wall. Using these locations we can edit elements in the document. That is we can set a new location point, or location curve to an element. The location can be retrieved from an element, by accessing it's location property, which will return a location class. So in a new command let's have a look, at setting a location of a point based family. In the visual studio exercise file for this video, I've gone ahead and created a change location class. This is an IExternalCommand, which will allow the user, to select an element and then up retrieve, the element of ele. I then created and empty transaction, with start and commit methods. Within here let's try retrieving a location point of an, element and then setting a new one. When a user selects an element we want to be able, to move it based on whether it's a, location point or location curve. So let's start by retrieving a location as a location point, by creating a location point variable named locp, and we'll assign to this a call to the location property, from the element. This returns a location class, so we need to cast it, to a location point using the key word as. If a user selects a wall which is a location curve, this will return null as the as key word, will return null if it fails. So let's check if locp is null or not. If it's not null then we know we have a location point. To do that let's use an if statement to check if, locp does not equal null. If this is a point then this will result in true, so then let's set a new location point. First we need to retrieve the current location, by creating an XYZ variable named loc. And then assigning to this the, point form the location point. We can do the be accessing the point property on the object. So locp, dot point. Now let's create a new XYZ, which will be the, new log and we will use it to create a new XYZ. This will be the new location point. To set it to a new point let's simply move it, three units in the X axis from the original point. So that will be loc X plus three. Loc Y for the Y parameter. And loc dot zed for the Z parameter. Perfect, so to change the location of an element, we simply re-assign the location, point property, to our new XYZ. On the next line, let's simply access locp dot Point, equals new loc, and if the command has gone to this piont, let's commit it. So we can commit the new location to the model, by accessing the commit method from the trans object. Great, so our new command will allow the user, to select an object, try to retrieve the objects location, as a location point, and if it can do so, it will set the new location point. I've already added this into the manifest file, so let's go ahead and debug the command. And open up the Revit exercise file for this video, and let's try the command on the piece of furniture. Go ahead to Add-Ins external tools, and there's our command change location. And it looks like we get the error, that the transaction has not started yet. So let's go back to our code and, stop the debugging, and it looks like it has occurred because, we have two commits. So if our first commit succeed, the second commit doesn't have a transaction started. To fix this let's remove this line, and we'll cut this first transaction, and put it inside the if statement. So now this will only occur if we, have selected a location point. Let's hit start to debug that, and try our command again. Perfect, you can see that it moved, three units in the X axis. Changing the location is just one, of the many ways to edit elements. So next let's have a look at a few more.

Contents