明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 689|回复: 1

[图形系统] Canvas,二开动画

[复制链接]
发表于 2025-9-14 21:54:38 | 显示全部楼层 |阅读模式
  1. using HTJ.Models;
  2. using HTJ.Views;
  3. using HTJ.Arx.Service.Interface;
  4. using Timer = System.Threading.Timer;

  5. namespace HTJ.ViewModels;

  6. public partial class TestViewModel : ObservableObject
  7. {
  8.     private static readonly IUserSetting IuserSetting = AppX.IuserSetting;

  9.     [ObservableProperty] private TestModel _vmData = IuserSetting.GetService<TestModel>();

  10.     private bool increasing;
  11.    
  12.     private double curRadius;
  13.    
  14.     private double ang;
  15.    
  16.     private Timer _timer;
  17.    

  18.     [RelayCommand]
  19.     private void Sel()
  20.     {
  21.         var view = AppHelper.GetService<TestView>();
  22.         view.Hide();
  23.         using var tr = new DBTrans(docLock: true);
  24.         if (!Env.Editor.SelEnt(out DBText text, true, "请选择需要搜素的文字"))
  25.             return;
  26.         VmData.Name = text.TextString;
  27.         view.ShowDialog();
  28.     }

  29.     [RelayCommand]
  30.     private void Save()
  31.     {
  32.         IuserSetting.Save(VmData);
  33.     }

  34.     [RelayCommand]
  35.     private void Read()
  36.     {
  37.         IuserSetting.ReLoad(VmData);
  38.     }

  39.     [RelayCommand]
  40.     private void Add()
  41.     {
  42.         VmData.Value += 5;
  43.         VmData.Name = $"Name{VmData.Value}";
  44.     }

  45.     [RelayCommand]
  46.     private void Open()
  47.     {
  48.         try
  49.         {
  50.             Process.Start("explorer.exe", AppX.AppPath);
  51.         }
  52.         catch (Exception e)
  53.         {
  54.             Console.WriteLine(e);
  55.         }
  56.     }

  57.     [RelayCommand]
  58.     private void Stop()
  59.     {
  60.         var view = AppHelper.GetService<TestView>();
  61.         view.Hide();
  62.         Env.Document.SetFocus();
  63.         _timer.Dispose();
  64.         view.ShowDialog();
  65.         
  66.     }

  67.     [RelayCommand]
  68.     private void Render()
  69.     {
  70.         // 创建TimerCallback委托
  71.         TimerCallback callback = TimerTask;
  72.         // 创建一个状态对象,可以传递任何需要的数据
  73.         var state = new TimerState
  74.         {
  75.             Message = "定时器已触发",
  76.             Count = 0
  77.         };
  78.         // 设置初始延迟2秒,之后每隔1秒触发一次
  79.         TimeSpan dueTime = TimeSpan.FromSeconds(2);
  80.         TimeSpan period = TimeSpan.FromSeconds(0.01);
  81.         // 创建计时器
  82.         _timer = new Timer(callback, state, dueTime, period);
  83.         
  84.     }
  85.     // 计时器任务方法
  86.     void TimerTask(object state)
  87.     {
  88.         if (increasing)
  89.         {
  90.             curRadius+=1;
  91.             if (curRadius>=200)
  92.                 increasing = false;
  93.         }
  94.         else
  95.         {
  96.             curRadius-=1;
  97.             if (curRadius<=20)
  98.                 increasing = true;
  99.         }
  100.         VmData.Radius=curRadius;
  101.         VmData.Thick=15-curRadius/15;
  102.         // var ss=TimeSpan.FromSeconds(0.01);
  103.         ang+=1;
  104.         if (ang>360)
  105.             ang %= 360;
  106.         VmData.Ang = ang;
  107.         var rad = ang.ToRadian();
  108.         var x=150+100*Math.Cos(rad);
  109.         var y=150+100*Math.Sin(rad);
  110.         
  111.         VmData.X = x;
  112.         VmData.Y = y;
  113.         // var timerState = (TimerState)state;
  114.         // // 模拟一些工作
  115.         // Thread.Sleep(300); // 300毫秒
  116.     }
  117.    
  118.     // 状态类,用于传递数据给计时器回调
  119.     public class TimerState
  120.     {
  121.         public string? Message { get; set; }
  122.         public int Count { get; set; }
  123.     }
  124.    
  125. }



  1. using System.Windows.Media;
  2. using HTJ.Arx.Service.Interface;
  3. using Point = System.Windows.Point;
  4. using Rect = System.Windows.Rect;


  5. namespace HTJ.Models;

  6. [AppHelper.Service]
  7. public partial class TestModel : ObservableObject, IUser
  8. {
  9.     [NotifyPropertyChangedFor(nameof(NameStr))]
  10.     [ObservableProperty] private string _name = "KZ";

  11.     [NotifyPropertyChangedFor(nameof(NameStr))]
  12.     [NotifyPropertyChangedFor(nameof(Geo))]
  13.     [ObservableProperty] private double _value = 10;
  14.    
  15.     [NotifyPropertyChangedFor(nameof(RadiusCent))]
  16.     [ObservableProperty] private double _radius = 10;
  17.    
  18.     [ObservableProperty] private double _thick = 10;
  19.    
  20.     [ObservableProperty] private double _ang = 10;
  21.    
  22.     [ObservableProperty] private double _x = 10;
  23.     [ObservableProperty] private double _y = 10;
  24.    
  25.     public double RadiusCent => -0.5*Radius;
  26.    
  27.     public System.Windows.Media.Color Color { get; set; } = Colors.Blue;
  28.     public string NameStr => Name + Value;

  29.     public GeometryGroup Geo => GetGeo();
  30.    
  31.     private GeometryGroup GetGeo()
  32.     {
  33.         var geos = new GeometryGroup();
  34.         
  35.         // 添加多个几何图形到组中
  36.         geos.Children.Add(new EllipseGeometry(new Point(50, 50), Value, 80));
  37.         
  38.         
  39.         geos.Children.Add(new RectangleGeometry(new Rect(0, 0, 20, 20)));
  40.         
  41.         // 添加一条线
  42.         var line = new LineGeometry(new Point(200, 200), new Point(300, 100));
  43.         geos.Children.Add(line);
  44.         
  45.         // 添加自定义路径
  46.         var customPath = new PathGeometry();
  47.         
  48.         var figure = new PathFigure();
  49.         figure.StartPoint = new Point(150, 150);
  50.         figure.Segments.Add(new LineSegment(new Point(200, 200), true));
  51.         figure.Segments.Add(new LineSegment(new Point(100, 200), true));
  52.         figure.IsClosed = true;
  53.         figure.IsFilled = true;
  54.         customPath.Figures.Add(figure);
  55.         
  56.         geos.Children.Add(customPath);
  57.         
  58.         return geos;
  59.     }
  60.     // 随机线条颜色
  61.   
  62.    
  63. }


  1. <Window x:Class="HTJ.Views.TestView"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6.         xmlns:vm="clr-namespace:HTJ.ViewModels"
  7.         mc:Ignorable="d"
  8.         Topmost="True"
  9.         Title="TestView" Height="500" Width="400">
  10.     <Window.DataContext>
  11.         <vm:TestViewModel />
  12.     </Window.DataContext>
  13.     <Window.Resources>
  14.         <ResourceDictionary>
  15.             <ResourceDictionary.MergedDictionaries>
  16.                 <ResourceDictionary Source="..\styles\styles.xaml" />
  17.             </ResourceDictionary.MergedDictionaries>
  18.         </ResourceDictionary>
  19.     </Window.Resources>
  20.     <StackPanel Margin="10">
  21.         <TextBox Margin="0,2" Text="{Binding VmData.Name,UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource LabelTextBox}"  Height="30" Tag="字符串" />
  22.         <TextBox Margin="0,2" Text="{Binding VmData.Value,UpdateSourceTrigger=PropertyChanged}" Height="30" Tag="100" />
  23.         <Separator Height="20" Background="Red" />
  24.         <StackPanel Orientation="Horizontal">
  25.             <TextBlock Text="" FontSize="14" FontWeight="Bold" Margin="0,3" />
  26.             <TextBlock Text="{Binding VmData.NameStr}" FontSize="14" FontWeight="Bold" Margin="0,3" HorizontalAlignment="Right" />
  27.         </StackPanel>
  28.         <Separator Height="20" Background="Red" />
  29.         <StackPanel Orientation="Horizontal">
  30.             <Button Content="拾取" Command="{Binding SelCommand}" />
  31.             <Button Content="添加" Command="{Binding AddCommand}" />
  32.             <Button Content="保存" Command="{Binding SaveCommand}" />
  33.             <Button Content="重载" Command="{Binding ReadCommand}" />
  34.             <Button Content="打开" Command="{Binding OpenCommand}" />
  35.             <Button Content="渲染" Command="{Binding RenderCommand}" />
  36.             <Button Content="停止" Command="{Binding StopCommand}" />
  37.         </StackPanel>
  38.         <Canvas Background="SandyBrown"
  39.                 Width="300" Height="300"
  40.                 HorizontalAlignment="Left"
  41.                 DataContext="{Binding VmData}">
  42.             <TextBlock Text="{Binding NameStr}"
  43.                        Canvas.Left="{Binding X}"
  44.                        Canvas.Top="{Binding Y}"
  45.                        FontSize="15"
  46.                        FontStyle="Normal"
  47.                        FontWeight="Bold"
  48.                        Foreground="{Binding Color}">
  49.                 <TextBlock.RenderTransform>
  50.                     <RotateTransform Angle="{Binding Ang}"/>
  51.                 </TextBlock.RenderTransform>
  52.             </TextBlock>
  53.             <Ellipse Width="250" Height="250"  
  54.                      Canvas.Left="150" Canvas.Top="150"
  55.                      StrokeThickness="1"
  56.                      Stroke="White">
  57.                 <Ellipse.RenderTransform>
  58.                     <TranslateTransform X="-125"
  59.                                         Y="-125">
  60.                     </TranslateTransform>
  61.                 </Ellipse.RenderTransform>
  62.             </Ellipse>
  63.             <Ellipse Width="{Binding Radius}"
  64.                      Height="{Binding Radius}"  
  65.                      Canvas.Left="150" Canvas.Top="150"
  66.                      StrokeThickness="{Binding Thick}"
  67.                      StrokeDashArray="1,4"
  68.                      Stroke="Red">
  69.                 <Ellipse.RenderTransform>
  70.                     <TranslateTransform X="{Binding RadiusCent}"
  71.                                         Y="{Binding RadiusCent}">
  72.                     </TranslateTransform>
  73.                 </Ellipse.RenderTransform>
  74.             </Ellipse>
  75.             <Ellipse Width="2" Height="2"  
  76.                      Canvas.Left="{Binding X}"
  77.                      Canvas.Top="{Binding Y}"
  78.                      StrokeThickness="5"
  79.                      Stroke="GreenYellow">
  80.                 <Ellipse.RenderTransform>
  81.                     <TranslateTransform X="-1"
  82.                                         Y="-1">
  83.                     </TranslateTransform>
  84.                 </Ellipse.RenderTransform>
  85.             </Ellipse>
  86.             <Button Canvas.Right="10" Canvas.Top="10"
  87.                     Padding="10,5"
  88.                     BorderBrush="Transparent">
  89.             </Button>
  90.             <!-- <Path Data="{Binding Geo}"  -->
  91.             <!--       Stroke="Fuchsia"  -->
  92.             <!--       StrokeThickness="1"  -->
  93.             <!--       Fill="Red"></Path> -->
  94.         </Canvas>
  95.     </StackPanel>
  96. </Window>


  1. namespace HTJ.Views;

  2. [AppHelper.Service]
  3. public partial class TestView
  4. {
  5.     public TestView()
  6.     {
  7.         InitializeComponent();
  8.     }

  9.     //窗体关闭事件
  10.     protected override void OnClosing(CancelEventArgs e)
  11.     {
  12.         e.Cancel = true;
  13.         Hide();
  14.     }
  15. }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
回复

使用道具 举报

发表于 2025-9-19 21:33:11 来自手机 | 显示全部楼层
这是用来做啥的?就动画效果么
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-10-6 01:43 , Processed in 0.171005 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表