简体   繁体   中英

How can I run built-in Revit commands from C#

I am wanting to know if there is a methodology to feed calculated values to a built-in Revit command from inside a C# program, and then possibly (based on results, such as whether this makes an element too short or too long for a known "maximum span" of a particular beam) continue with my C# program and change the beam size). I am told you can invoke the Revit built-in command after execution of your c# external command, but you cannot then return to the c# program

As another example, I want to select an element to trim/extend to, and have the code figure out which "Joist" beams to extend to this element. My program would do extended filtering (such as "Reference Level", or "Workset", or "Comments", or "Mark" parameters (etc.)) and then run the built in function, providing the element to extend to and then each of my beams.

I've tried internet searches, as well as the Revit SDK samples, and nothing obviously used this (but there are a lot of csproj's to look through).

Can anyone verify that you cannot go back and forth between the C# program and the Revit built-in command?

You can programmatically invoke a built in Revit command with the UIApplication.PostCommand() method. Refer to documentation and building coder for more information. It will not execute until after the API context is over, however.

I don't think you'll be able to feed arguments into the command however, short of some kind of Win32 hack. Perhaps you will need to recreate the functionality of the built in command within the Revit API.

Unfortunately, I don't think we can do (command "_line" pnt1 pnt2) type of thing here.

Perhaps start with the SDK sample "MoveLinear". It shows how to modify end points of linear elements (which includes beams).

The main part of the sample's code is

                Autodesk.Revit.DB.Line line;
                //get start point via "get_EndPoint(0)"
                Autodesk.Revit.DB.XYZ newStart = new XYZ(
                    lineLoc.Curve.GetEndPoint(0).X + 100,
                    lineLoc.Curve.GetEndPoint(0).Y,
                    lineLoc.Curve.GetEndPoint(0).Z);
                //get end point via "get_EndPoint(1)"
                Autodesk.Revit.DB.XYZ newEnd = new XYZ(
                    lineLoc.Curve.GetEndPoint(1).X,
                    lineLoc.Curve.GetEndPoint(1).Y + 100,
                    lineLoc.Curve.GetEndPoint(1).Z);
                //get a new line and use it to move current element 
                //with property "Autodesk.Revit.DB.LocationCurve.Curve"
                line = Line.CreateBound(newStart, newEnd);
                lineLoc.Curve = line;

Which moves the X of the first point and the Y of the second point 100 feet.

you can try:

 RevitCommandId commandId = RevitCommandId.LookupPostableCommandId(PostableCommand.PlaceAComponent);
 commandData.Application.PostCommand(commandId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM