Files
Shrlalgo.RvKits/ShrlAlgoToolkit.Revit/Assists/FunctionByGuidLine.cs

97 lines
3.2 KiB
C#
Raw Normal View History

2024-09-22 11:05:41 +08:00
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
2025-04-24 20:56:44 +08:00
namespace ShrlAlgoToolkit.Revit.Assists;
2024-09-22 11:05:41 +08:00
public class FunctionByGuidLine
{
public static Line GetGuideLine(Document doc, List<ElementId> eleIdsAdded)
{
var referLineId = eleIdsAdded.FirstOrDefault();
var guideLine = doc.GetElement(referLineId) as FamilyInstance;
var line = (guideLine?.Location as LocationCurve)?.Curve as Line;
doc.Delete(referLineId);
return line;
}
#region 线
private static readonly bool PlaceSingleInstanceAbort = true;
//只有一条线
private readonly List<ElementId> eleIdsRefer = new();
private IntPtr revitWindow;
public bool PlaceElementsByLine(UIApplication uiapp)
{
var uidoc = uiapp.ActiveUIDocument;
var app = uiapp.Application;
var doc = uidoc.Document;
#if REVIT2018
revitWindow = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
#else
revitWindow = uiapp.MainWindowHandle;
#endif
var f = new FilteredElementCollector(doc).OfClass(typeof(Family)).Cast<Family>().FirstOrDefault(x => x.Name == "LocationCurve");
var symbol = doc.GetElement(f.GetFamilySymbolIds().FirstOrDefault()) as FamilySymbol;
eleIdsRefer.Clear();
using var tg = new TransactionGroup(doc, "创建");
tg.Start();
try
{
//var x = UiDocument.GetPlacementTypes(symbol, doc.ActiveView);
//存在不能切换布置在平面上还是工作平面上的问题(如果族不需要切换,则没问题)
app.DocumentChanged += App_DocumentChanged;
uidoc.PromptForFamilyInstancePlacement(symbol);
app.DocumentChanged -= App_DocumentChanged;
//UiDocument.PostRequestForElementTypePlacement(symbol);
}
catch (OperationCanceledException)
{
var n = eleIdsRefer.Count;
//XYZ place = UiDocument.Selection.PickPoint("请点击布置的一侧");
if (n > 0)
{
using var ts = new Transaction(doc, "删除");
ts.Start();
var eid = eleIdsRefer.FirstOrDefault();
var l = uidoc.ActiveView.GenLevel;
if (doc.GetElement(eid) is FamilyInstance fi)
{
Wall.Create(doc, (fi.Location as LocationCurve)?.Curve, l.Id, false);
}
doc.Delete(eid);
//Debug.Print(ex.ViewMessage);
ts.Commit();
}
}
tg.Assimilate();
return true;
}
private void App_DocumentChanged(object sender, DocumentChangedEventArgs e)
{
var idsAdded = e.GetAddedElementIds();
var n = idsAdded.Count;
eleIdsRefer.AddRange(idsAdded);
if (PlaceSingleInstanceAbort && 0 < n)
{
//KeyIntPtrHelper.OneKey(_revit_window, (char)System.Windows.Forms.Keys.Escape);
KeyIntPtrHelper.OneKey(revitWindow, '\u001b');
//System.Windows.Forms.SendKeys.SendWait("^{F4}");
//KeyIntPtrHelper.SendKeys(Autodesk.Windows.ComponentManager.ApplicationWindow, System.Windows.Forms.Keys.Escape);
}
}
#endregion
}