调整代码
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
|
||||
using Autodesk.Revit.Attributes;
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using Autodesk.Revit.UI.Selection;
|
||||
|
||||
using Nice3point.Revit.Toolkit.External;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitAddins.Modeling;
|
||||
[Transaction(TransactionMode.Manual)]
|
||||
[Regeneration(RegenerationOption.Manual)]
|
||||
public class CADCurveToModelCurveCmd : ExternalCommand
|
||||
{
|
||||
public override void Execute()
|
||||
{
|
||||
|
||||
#region SelectExecute
|
||||
|
||||
using (Transaction trans = new Transaction(Document, "default"))
|
||||
{
|
||||
try
|
||||
{
|
||||
Reference refer = UiDocument.Selection.PickObject(ObjectType.Element, "请选择CAD");
|
||||
Element e = UiDocument.Document.GetElement(refer);
|
||||
if (e is ImportInstance ins)
|
||||
{
|
||||
trans.Start();
|
||||
var cadGeo = e.GetGeometryObjects().FirstOrDefault();
|
||||
List<GeometryObject> curves = [];
|
||||
if (cadGeo is GeometryInstance geometryInstance)
|
||||
{
|
||||
curves = [.. geometryInstance.GetInstanceGeometry()];
|
||||
}
|
||||
foreach (var c in curves)
|
||||
{
|
||||
|
||||
if (c is NurbSpline nurb)
|
||||
{
|
||||
if (nurb.Length > 100 / 304.8)
|
||||
{
|
||||
var points = nurb.Tessellate();
|
||||
ReferencePointArray array = new ReferencePointArray();
|
||||
foreach (var p in points)
|
||||
{
|
||||
var rp = Document.FamilyCreate.NewReferencePoint(p);
|
||||
array.Append(rp);
|
||||
}
|
||||
var curveBypoints = Document.FamilyCreate.NewCurveByPoints(array);
|
||||
}
|
||||
}
|
||||
if (c is PolyLine poly)
|
||||
{
|
||||
ReferencePointArray array = new ReferencePointArray();
|
||||
|
||||
for (int i = 0; i < poly.NumberOfCoordinates - 1; i++)
|
||||
{
|
||||
var p = poly.GetCoordinate(i);
|
||||
//Line.CreateBound(p, poly.GetCoordinate(i + 1));
|
||||
}
|
||||
}
|
||||
if (c is Line l)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
trans.Commit();
|
||||
}
|
||||
|
||||
}
|
||||
catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
|
||||
{
|
||||
if (trans.GetStatus() == TransactionStatus.Started)
|
||||
{
|
||||
trans.Commit();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ErrorMessage = ex.Message;
|
||||
if (trans.GetStatus() == TransactionStatus.Started)
|
||||
{
|
||||
trans.RollBack();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion SelectExecute
|
||||
}
|
||||
}
|
||||
20
ShrlAlgoToolkit.RevitAddins/Modeling/InstanceCreatorCmd.cs
Normal file
20
ShrlAlgoToolkit.RevitAddins/Modeling/InstanceCreatorCmd.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Autodesk.Revit.Attributes;
|
||||
using Nice3point.Revit.Toolkit.External;
|
||||
using ShrlAlgoToolkit.RevitAddins.Common.Assists;
|
||||
using ShrlAlgoToolkit.RevitAddins.RvCommon;
|
||||
using ShrlAlgoToolkit;
|
||||
using ShrlAlgoToolkit.RevitAddins;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitAddins.Modeling;
|
||||
|
||||
/// <summary>
|
||||
/// Revit执行命令
|
||||
/// </summary>
|
||||
[Transaction(TransactionMode.Manual)]
|
||||
public class InstanceCreatorCmd : ExternalCommand
|
||||
{
|
||||
public override void Execute()
|
||||
{
|
||||
Common.Assists.WinDialogAssist.ShowOrActivate<InstanceCreatorView, InstanceCreatorViewModel>(UiDocument);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<ui:MelWindow
|
||||
x:Class="ShrlAlgoToolkit.RevitAddins.Modeling.InstanceCreatorView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local1="clr-namespace:ShrlAlgoToolkit.RevitAddins.Modeling"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:ui="https://github.com/ShrlAlgo/Melskin"
|
||||
Title="族实例布置"
|
||||
Width="220"
|
||||
Height="340"
|
||||
MinHeight="300"
|
||||
d:DataContext="{d:DesignInstance local1:InstanceCreatorViewModel}"
|
||||
Icon="{DynamicResource RevitIcon}"
|
||||
mc:Ignorable="d">
|
||||
<Window.Resources>
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceDictionary Source="pack://application:,,,/ShrlAlgoToolkit.RevitAddins;component/WPFUI.xaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
</Window.Resources>
|
||||
<ui:StackPanel Margin="5" Spacing="5">
|
||||
<ComboBox
|
||||
ui:InputAssist.PlaceholderText="选择族"
|
||||
DisplayMemberPath="Name"
|
||||
ItemsSource="{Binding Families}"
|
||||
SelectedItem="{Binding SelectedFamily, UpdateSourceTrigger=PropertyChanged}" />
|
||||
<ComboBox
|
||||
ui:InputAssist.PlaceholderText="选择族类型"
|
||||
DisplayMemberPath="Name"
|
||||
ItemsSource="{Binding FamilySymbols, Mode=OneWay}"
|
||||
SelectedItem="{Binding SelectedFamilySymbol, UpdateSourceTrigger=PropertyChanged}" />
|
||||
<Image
|
||||
Grid.Row="2"
|
||||
Width="128"
|
||||
Height="128"
|
||||
Source="{Binding Image}" />
|
||||
<TextBox
|
||||
Grid.Row="3"
|
||||
ui:InputAssist.Prefix="偏移量:"
|
||||
ui:InputAssist.Suffix="mm"
|
||||
Text="{Binding Offset}">
|
||||
<!--<TextBox.Text>
|
||||
<Binding Path="Offset" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<rules:DoubleValidationRule xmlns:rules="clr-namespace:ShrlAlgo.RvKits.ValidationRules" ValidatesOnTargetUpdated="True" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>-->
|
||||
</TextBox>
|
||||
<Button
|
||||
Grid.Row="4"
|
||||
HorizontalAlignment="Stretch"
|
||||
Command="{Binding PlaceInstancesCommand}"
|
||||
Content="布置"
|
||||
ToolTip="选择dwg块并布置族实例" />
|
||||
</ui:StackPanel>
|
||||
</ui:MelWindow>
|
||||
@@ -0,0 +1,16 @@
|
||||
using ShrlAlgoToolkit.RevitAddins.RvCommon;
|
||||
using ShrlAlgoToolkit;
|
||||
using ShrlAlgoToolkit.RevitAddins;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitAddins.Modeling;
|
||||
|
||||
/// <summary>
|
||||
/// InstanceCreatorView.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class InstanceCreatorView
|
||||
{
|
||||
public InstanceCreatorView()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
285
ShrlAlgoToolkit.RevitAddins/Modeling/InstanceCreatorViewModel.cs
Normal file
285
ShrlAlgoToolkit.RevitAddins/Modeling/InstanceCreatorViewModel.cs
Normal file
@@ -0,0 +1,285 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Media.Imaging;
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.DB.Structure;
|
||||
using Autodesk.Revit.UI.Selection;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Nice3point.Revit.Toolkit.External.Handlers;
|
||||
|
||||
using ShrlAlgoToolkit.RevitAddins.Common.Extensions;
|
||||
using ShrlAlgoToolkit.RevitAddins.RvCommon;
|
||||
using ShrlAlgoToolkit;
|
||||
using ShrlAlgoToolkit.RevitAddins;
|
||||
|
||||
|
||||
// ReSharper disable ConditionIsAlwaysTrueOrFalse
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitAddins.Modeling;
|
||||
|
||||
public partial class InstanceCreatorViewModel : ObservableObject
|
||||
{
|
||||
public InstanceCreatorViewModel(UIDocument uiDocument)
|
||||
{
|
||||
this.uiDocument = uiDocument;
|
||||
document = uiDocument.Document;
|
||||
uiDocument.Application.Application.FamilyLoadedIntoDocument += Application_FamilyLoadedIntoDocument;
|
||||
GetAvailableFamilies();
|
||||
}
|
||||
|
||||
private readonly UIDocument uiDocument;
|
||||
|
||||
~InstanceCreatorViewModel()
|
||||
{
|
||||
uiDocument.Application.Application.FamilyLoadedIntoDocument -= Application_FamilyLoadedIntoDocument;
|
||||
}
|
||||
private void GetAvailableFamilies()
|
||||
{
|
||||
#if REVIT2018 || REVIT2020
|
||||
//过滤掉栏杆族
|
||||
var collector = document
|
||||
.OfClass<Family>()
|
||||
.Cast<Family>()
|
||||
.Where(
|
||||
f =>
|
||||
f.IsEditable
|
||||
&& !f.IsInPlace
|
||||
&& f.GetFamilySymbolIds().Any()
|
||||
&& f.FamilyCategory.Id.IntegerValue is not (-2000127 or -2009013 or -2005301 or -2009060)
|
||||
&& f.FamilyPlacementType
|
||||
is FamilyPlacementType.OneLevelBased
|
||||
or FamilyPlacementType.TwoLevelsBased
|
||||
or FamilyPlacementType.WorkPlaneBased
|
||||
or FamilyPlacementType.OneLevelBasedHosted
|
||||
)
|
||||
.OrderBy(f => f.Name)
|
||||
.ToList();
|
||||
#elif REVIT2025
|
||||
//过滤掉栏杆族
|
||||
var collector = document
|
||||
.OfClass<Family>()
|
||||
.Cast<Family>()
|
||||
.Where(
|
||||
f =>
|
||||
f.IsEditable
|
||||
&& !f.IsInPlace
|
||||
&& f.GetFamilySymbolIds().Any()
|
||||
&& f.FamilyCategory.Id.Value is not (-2000127 or -2009013 or -2005301 or -2009060)
|
||||
&& f.FamilyPlacementType
|
||||
is FamilyPlacementType.OneLevelBased
|
||||
or FamilyPlacementType.TwoLevelsBased
|
||||
or FamilyPlacementType.WorkPlaneBased
|
||||
or FamilyPlacementType.OneLevelBasedHosted
|
||||
)
|
||||
.OrderBy(f => f.Name)
|
||||
.ToList();
|
||||
#endif
|
||||
Families = collector;
|
||||
}
|
||||
|
||||
private void Application_FamilyLoadedIntoDocument(object sender, Autodesk.Revit.DB.Events.FamilyLoadedIntoDocumentEventArgs e)
|
||||
{
|
||||
GetAvailableFamilies();
|
||||
}
|
||||
|
||||
private readonly ActionEventHandler handler = new();
|
||||
|
||||
[ObservableProperty]
|
||||
public partial BitmapSource Image { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyCanExecuteChangedFor(nameof(Modeling.InstanceCreatorViewModel.PlaceInstancesCommand))]
|
||||
public partial bool CanPlaceInstance { get; set; } = true;
|
||||
|
||||
private readonly Document document;
|
||||
|
||||
[ObservableProperty]
|
||||
public partial double Offset { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
public partial Family SelectedFamily { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyCanExecuteChangedFor(nameof(Modeling.InstanceCreatorViewModel.PlaceInstancesCommand))]
|
||||
private FamilySymbol selectedFamilySymbol;
|
||||
|
||||
[ObservableProperty]
|
||||
private List<Family> families;
|
||||
|
||||
[ObservableProperty]
|
||||
private List<FamilySymbol> familySymbols;
|
||||
|
||||
private bool CanPlace()
|
||||
{
|
||||
return SelectedFamilySymbol != null && CanPlaceInstance;
|
||||
}
|
||||
[RelayCommand(CanExecute = nameof(CanPlace))]
|
||||
private void PlaceInstances()
|
||||
{
|
||||
CanPlaceInstance = false;
|
||||
handler.Raise(_ =>
|
||||
{
|
||||
var family = SelectedFamily;
|
||||
var symbol = SelectedFamilySymbol;
|
||||
var d = Offset / 304.8;
|
||||
Reference reference;
|
||||
try
|
||||
{
|
||||
reference = uiDocument.Selection.PickObject(ObjectType.PointOnElement, new DwgElementSelection(), "请选择dwg链接的块参照");
|
||||
}
|
||||
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
||||
{
|
||||
CanPlaceInstance = true;
|
||||
return;
|
||||
}
|
||||
|
||||
//图层ID;
|
||||
//Document.HideDwgLayer(referenceFunc, out ElementId columnGraphicStyleId, out GeometryElement columnGeoElem);
|
||||
var dwg = document.GetElement(reference) as ImportInstance;
|
||||
var geoInstances = dwg.GetBlocksByRef(reference);
|
||||
var dwgTransform = dwg!.GetTotalTransform();
|
||||
if (geoInstances == null || !geoInstances.Any())
|
||||
{
|
||||
MessageBox.Show("选中块为子块或选中块的嵌套层级过深,请使用Tab来选择整体快", "识别失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
CanPlaceInstance = true;
|
||||
return;
|
||||
}
|
||||
|
||||
document.Invoke(
|
||||
_ =>
|
||||
{
|
||||
if (!symbol.IsActive)
|
||||
{
|
||||
symbol.Activate();
|
||||
}
|
||||
|
||||
switch (family.FamilyPlacementType)
|
||||
{
|
||||
case FamilyPlacementType.OneLevelBased:
|
||||
foreach (var ins in geoInstances)
|
||||
{
|
||||
var level = document.ActiveView.GenLevel;
|
||||
var loc = ins.GetLocationPoint(dwgTransform, out var rotation);
|
||||
|
||||
var familyInstance = document.Create.NewFamilyInstance(loc, symbol, level, StructuralType.NonStructural);
|
||||
document.Regenerate();
|
||||
ElementTransformUtils.RotateElement(document, familyInstance.Id, Line.CreateUnbound(loc, XYZ.BasisZ), rotation);
|
||||
document.Regenerate();
|
||||
var translation = XYZ.BasisZ * d;
|
||||
ElementTransformUtils.MoveElement(document, familyInstance.Id, translation);
|
||||
}
|
||||
|
||||
break;
|
||||
case FamilyPlacementType.OneLevelBasedHosted:
|
||||
#if REVIT2018 || REVIT2020
|
||||
if (family.FamilyCategory.Id.IntegerValue is -2000023 or -2000014)
|
||||
#elif REVIT2025
|
||||
if (family.FamilyCategory.Id.Value is -2000023 or -2000014)
|
||||
#endif
|
||||
{
|
||||
var level = document.ActiveView.GenLevel;
|
||||
foreach (var ins in geoInstances)
|
||||
{
|
||||
var loc = ins.GetLocationPoint(dwgTransform, out var rotation);
|
||||
|
||||
var minPoint = loc - new XYZ(0.4, 0.4, -level.Elevation);
|
||||
var maxPoint = loc + new XYZ(0.4, 0.4, level.Elevation + 1);
|
||||
var outline = new Outline(minPoint, maxPoint);
|
||||
var intersectsFilter = new BoundingBoxIntersectsFilter(outline);
|
||||
var intersect = document.OfClass<Wall>().WherePasses(intersectsFilter).FirstElement() as Wall;
|
||||
var point = loc + (XYZ.BasisZ * level.Elevation);
|
||||
document.Create.NewFamilyInstance(point, symbol, intersect, level, StructuralType.NonStructural);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case FamilyPlacementType.TwoLevelsBased:
|
||||
foreach (var ins in geoInstances)
|
||||
{
|
||||
var level = document.ActiveView.GenLevel;
|
||||
var loc = ins.GetLocationPoint(dwgTransform, out var rotation);
|
||||
|
||||
var familyInstance = document.Create.NewFamilyInstance(loc, symbol, level, StructuralType.NonStructural);
|
||||
document.Regenerate();
|
||||
ElementTransformUtils.RotateElement(document, familyInstance.Id, Line.CreateUnbound(loc, XYZ.BasisZ), rotation);
|
||||
document.Regenerate();
|
||||
var translation = XYZ.BasisZ * d;
|
||||
ElementTransformUtils.MoveElement(document, familyInstance.Id, translation);
|
||||
}
|
||||
|
||||
break;
|
||||
case FamilyPlacementType.WorkPlaneBased:
|
||||
foreach (var ins in geoInstances)
|
||||
{
|
||||
var loc = ins.GetLocationPoint(dwgTransform, out var rotation);
|
||||
var familyInstance = document.Create.NewFamilyInstance(
|
||||
loc,
|
||||
symbol,
|
||||
document.ActiveView.SketchPlane,
|
||||
StructuralType.NonStructural
|
||||
);
|
||||
document.Regenerate();
|
||||
ElementTransformUtils.RotateElement(document, familyInstance.Id, Line.CreateUnbound(loc, XYZ.BasisZ), rotation);
|
||||
document.Regenerate();
|
||||
var translation = XYZ.BasisZ * d;
|
||||
ElementTransformUtils.MoveElement(document, familyInstance.Id, translation);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
CanPlaceInstance = true;
|
||||
},
|
||||
"实例布置"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
//private static Dictionary<XYZ, string> WriteCode(UIApplication uiapp)
|
||||
//{
|
||||
// var application = uiapp.Application;
|
||||
// var uiDocument = uiapp.ActiveUIDocument;
|
||||
// var doc = uiDocument.Document;
|
||||
// Reference textRefer;
|
||||
// try
|
||||
// {
|
||||
// //图层ID;
|
||||
// textRefer = uiDocument.Selection.PickObject(
|
||||
// ObjectType.PointOnElement,
|
||||
// new FuncFilter(e => e is ImportInstance import && import.IsLinked && doc.GetElement(e.GetTypeId()) is CADLinkType),
|
||||
// "请选择“链接(非导入)”的Cad文字图层"
|
||||
// );
|
||||
// }
|
||||
// catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
||||
// {
|
||||
// return null;
|
||||
// }
|
||||
// var dwg = doc.GetElement(textRefer) as ImportInstance;
|
||||
// var textLayerName = dwg.GetLayerName(textRefer);
|
||||
// var path = dwg.GetFilePath();
|
||||
// var dwgTransform = dwg.GetTotalTransform();
|
||||
|
||||
// using ACadSharp.IO.DwgReader reader = new(path);
|
||||
// var cadDocument = reader.Read();
|
||||
// return cadDocument.Entities
|
||||
// .OfType<ACadSharp.Entities.TextEntity>()
|
||||
// .Where(e => e.Layer.Name == textLayerName)
|
||||
// .ToDictionary(
|
||||
// v =>
|
||||
// {
|
||||
// var loc = dwgTransform.OfPoint(new XYZ(v.InsertPoint.X, v.InsertPoint.Y, v.InsertPoint.Z));
|
||||
// return new XYZ(loc.X, loc.Y, 0);
|
||||
// },
|
||||
// e => e.Value
|
||||
// );
|
||||
//}
|
||||
partial void OnSelectedFamilyChanged(Family value)
|
||||
{
|
||||
var doc = value.Document;
|
||||
FamilySymbols = value.GetFamilySymbolIds().Select(id => doc.GetElement(id)).OfType<FamilySymbol>().ToList();
|
||||
}
|
||||
|
||||
partial void OnSelectedFamilySymbolChanged(FamilySymbol value)
|
||||
{
|
||||
Image = value.GetPreviewImage(new System.Drawing.Size(128, 128)).ToBitmapSource();
|
||||
}
|
||||
}
|
||||
17
ShrlAlgoToolkit.RevitAddins/Modeling/PipesCreatorCmd.cs
Normal file
17
ShrlAlgoToolkit.RevitAddins/Modeling/PipesCreatorCmd.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Nice3point.Revit.Toolkit.External;
|
||||
using ShrlAlgoToolkit.RevitAddins.Common.Assists;
|
||||
using ShrlAlgoToolkit.RevitAddins.RvCommon;
|
||||
using ShrlAlgoToolkit;
|
||||
using ShrlAlgoToolkit.RevitAddins;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitAddins.Modeling;
|
||||
|
||||
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
|
||||
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
|
||||
public class PipesCreatorCmd : ExternalCommand
|
||||
{
|
||||
public override void Execute()
|
||||
{
|
||||
Common.Assists.WinDialogAssist.ShowOrActivate<Modeling.PipesCreatorView, Modeling.PipesCreatorViewModel>(Document);
|
||||
}
|
||||
}
|
||||
44
ShrlAlgoToolkit.RevitAddins/Modeling/PipesCreatorView.xaml
Normal file
44
ShrlAlgoToolkit.RevitAddins/Modeling/PipesCreatorView.xaml
Normal file
@@ -0,0 +1,44 @@
|
||||
<ui:MelWindow
|
||||
x:Class="ShrlAlgoToolkit.RevitAddins.Modeling.PipesCreatorView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local1="clr-namespace:ShrlAlgoToolkit.RevitAddins.Modeling"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:ui="https://github.com/ShrlAlgo/Melskin"
|
||||
Title="管道翻模"
|
||||
Width="250"
|
||||
Height="180"
|
||||
MinWidth="250"
|
||||
MinHeight="180"
|
||||
d:DataContext="{d:DesignInstance Type=local1:PipesCreatorViewModel}"
|
||||
Icon="{DynamicResource RevitIcon}"
|
||||
SizeToContent="Height"
|
||||
mc:Ignorable="d">
|
||||
<Window.Resources>
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceDictionary Source="pack://application:,,,/ShrlAlgoToolkit.RevitAddins;component/WPFUI.xaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
</Window.Resources>
|
||||
<ui:Grid ChildMargin="5" Columns="*">
|
||||
<ComboBox
|
||||
Grid.Row="0"
|
||||
ui:InputAssist.PlaceholderText="管线类型"
|
||||
DisplayMemberPath="Name"
|
||||
ItemsSource="{Binding PipeTypes}"
|
||||
SelectedItem="{Binding SelectedPipeType}" />
|
||||
<ComboBox
|
||||
Grid.Row="1"
|
||||
ui:InputAssist.PlaceholderText="系统类型"
|
||||
DisplayMemberPath="Name"
|
||||
ItemsSource="{Binding PipingSystemTypes}"
|
||||
SelectedItem="{Binding SelectedPipingSystemType}" />
|
||||
<Button
|
||||
Grid.Row="2"
|
||||
HorizontalAlignment="Stretch"
|
||||
Command="{Binding CreateCommand}"
|
||||
Content="创建" />
|
||||
</ui:Grid>
|
||||
</ui:MelWindow>
|
||||
@@ -0,0 +1,17 @@
|
||||
using ShrlAlgoToolkit.RevitAddins.RvCommon;
|
||||
using ShrlAlgoToolkit;
|
||||
using ShrlAlgoToolkit.RevitAddins;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitAddins.Modeling
|
||||
{
|
||||
/// <summary>
|
||||
/// PipesCreatorView.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class PipesCreatorView
|
||||
{
|
||||
public PipesCreatorView()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
272
ShrlAlgoToolkit.RevitAddins/Modeling/PipesCreatorViewModel.cs
Normal file
272
ShrlAlgoToolkit.RevitAddins/Modeling/PipesCreatorViewModel.cs
Normal file
@@ -0,0 +1,272 @@
|
||||
using System.Text;
|
||||
using ACadSharp.Entities;
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.DB.Mechanical;
|
||||
using Autodesk.Revit.DB.Plumbing;
|
||||
using Autodesk.Revit.UI.Selection;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Nice3point.Revit.Toolkit.External.Handlers;
|
||||
|
||||
using ShrlAlgoToolkit.RevitAddins.RvCommon;
|
||||
using ShrlAlgoToolkit;
|
||||
using ShrlAlgoToolkit.RevitAddins;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitAddins.Modeling;
|
||||
|
||||
public partial class PipesCreatorViewModel : ObservableObject
|
||||
{
|
||||
public PipesCreatorViewModel(Document document)
|
||||
{
|
||||
this.document = document;
|
||||
}
|
||||
|
||||
private readonly ActionEventHandler createHandler = new();
|
||||
private readonly Document document;
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyCanExecuteChangedFor(nameof(Modeling.PipesCreatorViewModel.CreateCommand))]
|
||||
public partial PipeType SelectedPipeType { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyCanExecuteChangedFor(nameof(Modeling.PipesCreatorViewModel.CreateCommand))]
|
||||
public partial PipingSystemType SelectedPipingSystemType { get; set; }
|
||||
|
||||
public IList<PipeType> PipeTypes => document.OfClass<PipeType>().Cast<PipeType>().ToList();
|
||||
public IList<PipingSystemType> PipingSystemTypes => document.OfClass<PipingSystemType>().Cast<PipingSystemType>().ToList();
|
||||
|
||||
private bool CanCreate()
|
||||
{
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
||||
return SelectedPipeType != null && SelectedPipingSystemType != null;
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanCreate))]
|
||||
private void Create()
|
||||
{
|
||||
createHandler.Raise(uiapp =>
|
||||
{
|
||||
var application = uiapp.Application;
|
||||
var uiDocument = uiapp.ActiveUIDocument;
|
||||
var doc = uiDocument.Document;
|
||||
Reference reference;
|
||||
Reference textRefer;
|
||||
try
|
||||
{
|
||||
reference = uiDocument.Selection.PickObject(
|
||||
ObjectType.PointOnElement,
|
||||
new FuncFilter(e => e is ImportInstance import && import.IsLinked && doc.GetElement(e.GetTypeId()) is CADLinkType),
|
||||
"请选择dwg管线图层"
|
||||
);
|
||||
|
||||
//图层ID;
|
||||
textRefer = uiDocument.Selection.PickObject(ObjectType.PointOnElement, new FuncFilter(e => e is ImportInstance import && import.IsLinked && doc.GetElement(e.GetTypeId()) is CADLinkType), "请选择管线标注文字");
|
||||
}
|
||||
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var dwg = doc.GetElement(reference) as ImportInstance;
|
||||
var pipeLayerName = dwg.GetLayerName(reference);
|
||||
var textLayerName = dwg.GetLayerName(textRefer);
|
||||
//SketchPlane sketchPlane = SketchPlane.Create(Document, Plane.CreateByNormalAndOrigin(XYZ.BasisZ, XYZ.Zero));
|
||||
|
||||
doc.Invoke(_ =>
|
||||
{
|
||||
var curve = dwg!.GetGeometryObjectFromReference(reference);
|
||||
var text = dwg.GetGeometryObjectFromReference(textRefer);
|
||||
|
||||
doc.ActiveView.SetCategoryHidden(curve.GraphicsStyleId, false);
|
||||
doc.ActiveView.SetCategoryHidden(text.GraphicsStyleId, false);
|
||||
});
|
||||
var path = dwg.GetFilePath();
|
||||
using ACadSharp.IO.DwgReader reader = new(path);
|
||||
var cadDocument = reader.Read();
|
||||
//var blocks = cadDocument.Entities.OfType<ACadSharp.Entities.Insert>().Where(e => e.Layer.Name == layerName).ToList();
|
||||
var textEntities = cadDocument.Entities.OfType<TextEntity>().Where(e => e.Layer.Name == textLayerName).ToList();
|
||||
var lwPolyLines = cadDocument.Entities.OfType<LwPolyline>().Where(e => e.Layer.Name == pipeLayerName).ToList();
|
||||
var dwgTransform = dwg!.GetTransform();
|
||||
StringBuilder sb = new();
|
||||
doc.InvokeGroup(_ =>
|
||||
{
|
||||
foreach (var lwPolyline in lwPolyLines)
|
||||
{
|
||||
//if (n % 500 == 0)
|
||||
//{
|
||||
// var result = TaskDialog.Show("提示", $"已创建{n},是否继续?", TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No);
|
||||
// if (result == TaskDialogResult.No)
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
//}
|
||||
|
||||
var v = lwPolyline.Vertices;
|
||||
if (v.Count == 2)
|
||||
{
|
||||
var p1 = new XYZ(v[0].Location.X, v[0].Location.Y, 0) / 304.8;
|
||||
var p2 = new XYZ(v[1].Location.X, v[1].Location.Y, 0) / 304.8;
|
||||
if (p1.DistanceTo(p2) < application.ShortCurveTolerance)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var di = double.MaxValue;
|
||||
TextEntity text = null;
|
||||
var l = Autodesk.Revit.DB.Line.CreateBound(p1, p2);
|
||||
foreach (var entity in textEntities)
|
||||
{
|
||||
try
|
||||
{
|
||||
var transform = Transform.CreateRotation(XYZ.BasisZ, entity.Rotation);
|
||||
var rotationVector = transform.OfPoint(XYZ.BasisX);
|
||||
if (entity.Value.StartsWith("DN") || entity.Value.StartsWith("De"))
|
||||
{
|
||||
var crossProduct = l.Direction.CrossProduct(rotationVector);
|
||||
if (crossProduct.GetLength() < 0.05)
|
||||
{
|
||||
var xyz = new XYZ(entity.InsertPoint.X, entity.InsertPoint.Y, entity.InsertPoint.Z) / 304.8;
|
||||
var ofPoint = dwgTransform.OfPoint(xyz);
|
||||
//同一平面
|
||||
var point = new XYZ(ofPoint.X, ofPoint.Y, 0);
|
||||
var d = l.Distance(point);
|
||||
if (d < di)
|
||||
{
|
||||
di = d;
|
||||
text = entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
doc.Invoke(
|
||||
_ =>
|
||||
{
|
||||
//var pipeDiameter = Regex.Match(text.Value, @"(?<=DN)\d+");
|
||||
//if (pipeDiameter.Length > 0)
|
||||
//{
|
||||
// var diameter = Convert.ToDouble(pipeDiameter.Value) / 304.8;
|
||||
// pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(diameter);
|
||||
//}
|
||||
|
||||
//var elevation = Regex.Match(text.Value, @"(?<=\+)(\d|\.)+");
|
||||
//if (elevation.Length > 0)
|
||||
//{
|
||||
// var h = Convert.ToDouble(elevation.Value) * 1000 / 304.8;
|
||||
// pipe.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).Set(h);
|
||||
//}
|
||||
p1 = dwgTransform.OfPoint(p1);
|
||||
p2 = dwgTransform.OfPoint(p2);
|
||||
var newPipe = Pipe.Create(doc, SelectedPipingSystemType.Id, SelectedPipeType.Id, doc.ActiveView.GenLevel.Id, p1, p2);
|
||||
doc.Regenerate();
|
||||
|
||||
if (text != null)
|
||||
{
|
||||
var diameter = string.Empty;
|
||||
//string slope = string.Empty;
|
||||
//var splitStrings = text.Value.Split(' ');
|
||||
//foreach (var str in splitStrings)
|
||||
//{
|
||||
// var str1 = str.Trim();
|
||||
// if (str1.Contains("DN"))
|
||||
// {
|
||||
// diameter = str.TrimStart("DN".ToCharArray());
|
||||
// }
|
||||
// else if (str1.Contains("De"))
|
||||
// {
|
||||
// diameter = str.TrimStart("De".ToCharArray());
|
||||
// }
|
||||
|
||||
// if (str1.Contains("i="))
|
||||
// {
|
||||
// slope = str.TrimStart("i=".ToCharArray());
|
||||
// }
|
||||
//}
|
||||
|
||||
if (double.TryParse(diameter, out var d))
|
||||
{
|
||||
newPipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(d / 304.8);
|
||||
}
|
||||
|
||||
//if (double.TryParse(slope, out var s))
|
||||
//{
|
||||
// newPipe.get_Parameter(BuiltInParameter.RBS_PIPE_SLOPE).Set(s);
|
||||
//}
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.AppendLine($"{newPipe.Id}");
|
||||
}
|
||||
|
||||
// ElementTransformUtils.MoveElement(Document, newPipe.Id, move);
|
||||
//n++;
|
||||
},
|
||||
"创建管线"
|
||||
);
|
||||
doc.Invoke(_ =>
|
||||
{
|
||||
var mepCurves = doc.OfClass<MEPCurve>()
|
||||
.Where(p => p is not InsulationLiningBase and not FlexPipe and not FlexDuct)
|
||||
.ToList();
|
||||
foreach (var mep in mepCurves)
|
||||
{
|
||||
foreach (var mep1 in mepCurves)
|
||||
{
|
||||
if (
|
||||
mep.Id != mep1.Id
|
||||
&& !mep.GetConnectors(true).IsEmpty
|
||||
&& !mep1.GetConnectors(true).IsEmpty
|
||||
&& mep.GetType() == mep1.GetType()
|
||||
)
|
||||
{
|
||||
var connectors = ConnectorExtensions.GetNearestConnectors(mep, mep1);
|
||||
if (connectors.Count == 2 && connectors[0].Origin.IsAlmostEqualTo(connectors[1].Origin))
|
||||
{
|
||||
connectors[0].ConnectByFitting(connectors[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (sb.Length > 0)
|
||||
{
|
||||
//var filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\问题管线.txt";
|
||||
//File.WriteAllText(filePath, sb.ToString());
|
||||
//WinDialogAssist.OpenFolderAndSelectFile(filePath);
|
||||
Common.Assists.LogAssist.WriteTextFile("问题管线", sb.ToString());
|
||||
}
|
||||
|
||||
//UiDocument.Selection.SetElementIds(ids);
|
||||
//using (Transaction name = new Transaction(Document, "tsname"))
|
||||
//{
|
||||
// name.Start();
|
||||
// for (int i = 0; i < 20; i++)
|
||||
// {
|
||||
// var block = blocks[i];
|
||||
// var loc = new XYZ(block.InsertPoint.X / 304.8, block.InsertPoint.Y / 304.8, 0);
|
||||
|
||||
// //var loc = dwg.GetTransform().Origin;
|
||||
|
||||
// if (!instance.Symbol.IsActive)
|
||||
// {
|
||||
// instance.Symbol.Activate();
|
||||
// }
|
||||
|
||||
// var fi = Document.Create.NewFamilyInstance(loc, instance.Symbol, view.GenLevel, StructuralType.NonStructural);
|
||||
// Document.Regenerate();
|
||||
// ElementTransformUtils.MoveElement(Document,fi.Id,move);
|
||||
// }
|
||||
// name.Commit();
|
||||
//}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user