<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: i have this problem, i need solution in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13193027#M1658</link>
    <description>&lt;P&gt;thank you i will try that way&lt;/P&gt;</description>
    <pubDate>Wed, 04 Dec 2024 17:11:28 GMT</pubDate>
    <dc:creator>dinhthit25</dc:creator>
    <dc:date>2024-12-04T17:11:28Z</dc:date>
    <item>
      <title>i have this problem, i need solution</title>
      <link>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192666#M1651</link>
      <description>&lt;P&gt;i have this error applying color to solid3d&lt;BR /&gt;i using C#.net framework 4.8 at visual studio 2022 when i use the method i alway have this error&lt;BR /&gt;Error applying color to face: Exception of type 'Autodesk.AutoCAD.BoundaryRepresentation.Exception' was thrown.&lt;/P&gt;&lt;P&gt;here my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.BoundaryRepresentation;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using _3dInterfacePaint.Views;

namespace _3dInterfacePaint.ViewModels
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private string _selectedColor1;
        private string _selectedColor2;
        private Autodesk.AutoCAD.Colors.Color _color1;
        private Autodesk.AutoCAD.Colors.Color _color2;

        public string SelectedColor1
        {
            get =&amp;gt; _selectedColor1;
            set { _selectedColor1 = value; OnPropertyChanged(nameof(SelectedColor1)); }
        }

        public string SelectedColor2
        {
            get =&amp;gt; _selectedColor2;
            set { _selectedColor2 = value; OnPropertyChanged(nameof(SelectedColor2)); }
        }

        public ICommand SelectColor1Command { get; }
        public ICommand SelectColor2Command { get; }
        public ICommand ApplyColorsCommand { get; }

        public MainWindowViewModel()
        {
            SelectColor1Command = new RelayCommand(_ =&amp;gt; SelectColor(1));
            SelectColor2Command = new RelayCommand(_ =&amp;gt; SelectColor(2));
            ApplyColorsCommand = new RelayCommand(_ =&amp;gt; ApplyColorsTo3DObject());
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private void SelectColor(int colorNumber)
        {
            var colorDialog = new Autodesk.AutoCAD.Windows.ColorDialog();
            if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (colorNumber == 1)
                {
                    _color1 = colorDialog.Color;
                    SelectedColor1 = $"First 1: {_color1.ColorIndex}";
                }
                else
                {
                    _color2 = colorDialog.Color;
                    SelectedColor2 = $"Second 2: {_color2.ColorIndex}";
                }
            }
        }

        private void ApplyColorsTo3DObject()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            if (_color1 == null || _color2 == null)
            {
                ed.WriteMessage("\nPlease choose 2 colors.");
                return;
            }

            using (var lck = doc.LockDocument())
            {
                PromptEntityOptions peo = new PromptEntityOptions("\nSelect 3D object:");
                peo.SetRejectMessage("\nNot Solid3d.");
                peo.AddAllowedClass(typeof(Solid3d), true);
                var per = ed.GetEntity(peo);

                if (per.Status != PromptStatus.OK)
                {
                    ed.WriteMessage("\nObject not found.");
                    return;
                }

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    Solid3d solid3d = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Solid3d;
                    if (solid3d == null)
                    {
                        ed.WriteMessage("\nSelected object is not a valid Solid3d.");
                        return;
                    }

                    var fullSubentityPath = new FullSubentityPath(new[] { per.ObjectId }, new SubentityId(SubentityType.Null, IntPtr.Zero));

                    using (var brep = new Brep(fullSubentityPath))
                    {
                        int faceIndex = 0;

                        var faces = brep.Faces.ToArray();
                        foreach (var face in faces)
                        {
                            try
                            {
                                ApplyStripesToFace(face, _color1, _color2);
                                faceIndex++;
                            }
                            catch (System.Exception ex)
                            {
                                ed.WriteMessage($"\nError applying color to face: {ex.Message}");
                            }
                        }
                    }

                    tr.Commit();
                }
            }
        }




        private void ApplyStripesToFace(Autodesk.AutoCAD.BoundaryRepresentation.Face face, Autodesk.AutoCAD.Colors.Color color1, Autodesk.AutoCAD.Colors.Color color2)
        {
            try
            {
                var loops = face.Loops.ToArray();
                if (loops.Length &amp;gt; 0)
                {
                    var loop = loops[0];  // Giả sử chỉ có một vòng lặp trên mỗi mặt

                    // Chia mặt thành các vùng sọc
                    var segments = SplitFaceIntoStripes(face, 5); // Chia thành 5 vùng (ví dụ)

                    for (int i = 0; i &amp;lt; segments.Count; i++)
                    {
                        var stripeFace = segments[i];
                        var stripeColor = (i % 2 == 0) ? color1 : color2;

                        // Áp dụng màu cho vùng
                        ApplyColorToFace(stripeFace, stripeColor);
                    }
                }
            }
            catch (System.Exception ex)
            {
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\nError creating stripes on face: {ex.Message}");
            }
        }



        private List&amp;lt;Autodesk.AutoCAD.BoundaryRepresentation.Face&amp;gt; SplitFaceIntoStripes(Autodesk.AutoCAD.BoundaryRepresentation.Face face, int stripeCount)
        {
            var stripes = new List&amp;lt;Autodesk.AutoCAD.BoundaryRepresentation.Face&amp;gt;();

            // Logic để chia mặt thành các vùng stripe (giả định chia đều theo chiều cao hoặc chiều dài)
            // TODO: Cần thêm logic chia chính xác dựa trên hình học của mặt

            for (int i = 0; i &amp;lt; stripeCount; i++)
            {
                stripes.Add(face); // Tạm thêm mặt gốc (để placeholder logic)
            }

            return stripes;
        }


        private void ApplyColorToFace(Autodesk.AutoCAD.BoundaryRepresentation.Face face, Autodesk.AutoCAD.Colors.Color color)
        {
            var subentityId = face.SubentityPath.SubentId;

            using (Transaction tr = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
            {
                Solid3d solid = tr.GetObject(face.SubentityPath.GetObjectIds().First(), OpenMode.ForWrite) as Solid3d;
                if (solid != null)
                {
                    solid.SetSubentityColor(subentityId, color);
                }

                tr.Commit();
            }
        }


        public class RelayCommand : ICommand
        {
            private readonly Action&amp;lt;object&amp;gt; _execute;
            private readonly Predicate&amp;lt;object&amp;gt; _canExecute;

            public RelayCommand(Action&amp;lt;object&amp;gt; execute, Predicate&amp;lt;object&amp;gt; canExecute = null)
            {
                _execute = execute ?? throw new ArgumentNullException(nameof(execute));
                _canExecute = canExecute;
            }



            public bool CanExecute(object parameter) =&amp;gt; _canExecute?.Invoke(parameter) ?? true;

            public void Execute(object parameter) =&amp;gt; _execute(parameter);

            public event EventHandler CanExecuteChanged
            {
                add =&amp;gt; CommandManager.RequerySuggested += value;
                remove =&amp;gt; CommandManager.RequerySuggested -= value;
            }
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 14:52:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192666#M1651</guid>
      <dc:creator>dinhthit25</dc:creator>
      <dc:date>2024-12-04T14:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: i have this problem, i need solution</title>
      <link>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192756#M1652</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;What do you expect with this code?&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;                    // Chia mặt thành các vùng sọc
                    var segments = SplitFaceIntoStripes(face, 5); // Chia thành 5 vùng (ví dụ)

                    for (int i = 0; i &amp;lt; segments.Count; i++)
                    {
                        var stripeFace = segments[i];
                        var stripeColor = (i % 2 == 0) ? color1 : color2;

                        // Áp dụng màu cho vùng
                        ApplyColorToFace(stripeFace, stripeColor);
                    }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The 'segments' variable is a list which will contain 5 times the same face; { face, face, face, face, face }&lt;/P&gt;
&lt;P&gt;So, when running the for loop you set the same face color, one time with color1, next time with color2 and so on until 5 times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should explain what you try to achieve with the ApplyColorTo3DObject method, and do not ned to post all the WPF related code which is not relevant for your issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 15:33:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192756#M1652</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-12-04T15:33:09Z</dc:date>
    </item>
    <item>
      <title>Re: i have this problem, i need solution</title>
      <link>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192769#M1653</link>
      <description>&lt;P&gt;Perhaps you could attach a picture showing what you want to get.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 15:38:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192769#M1653</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-12-04T15:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: i have this problem, i need solution</title>
      <link>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192822#M1654</link>
      <description>&lt;P&gt;thanks you for your advise, im trying to paint 2&amp;nbsp;color&amp;nbsp;alternate on every face and the code you ask i use to&amp;nbsp;divide a face of a 3D object in AutoCAD into stripes and apply alternating colors (stripe colors) to these regions(i forgot change the node to english, sorry).&lt;/P&gt;&lt;PRE&gt;// Chia mặt thành các vùng sọc
                    var segments = SplitFaceIntoStripes(face, 5); // Chia thành 5 vùng (ví dụ)

                    for (int i = 0; i &amp;lt; segments.Count; i++)
                    {
                        var stripeFace = segments[i];
                        var stripeColor = (i % 2 == 0) ? color1 : color2;

                        // Áp dụng màu cho vùng
                        ApplyColorToFace(stripeFace, stripeColor);
                    }&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 15:58:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192822#M1654</guid>
      <dc:creator>dinhthit25</dc:creator>
      <dc:date>2024-12-04T15:58:12Z</dc:date>
    </item>
    <item>
      <title>Re: i have this problem, i need solution</title>
      <link>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192874#M1655</link>
      <description>&lt;P&gt;You cannot "paint 2 color alternate on every face" by setting the face color. The Solid3d.SetSubentityColor method applies a single color to the whole subentity (Face, Edge or Vertex).&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 16:11:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192874#M1655</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-12-04T16:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: i have this problem, i need solution</title>
      <link>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192925#M1656</link>
      <description>&lt;P&gt;so that why it won't work thank you&lt;BR /&gt;and may i ask which is best way to&amp;nbsp;&lt;SPAN&gt;paint 2&amp;nbsp;color&amp;nbsp;alternate on every face&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 16:37:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192925#M1656</guid>
      <dc:creator>dinhthit25</dc:creator>
      <dc:date>2024-12-04T16:37:32Z</dc:date>
    </item>
    <item>
      <title>Re: i have this problem, i need solution</title>
      <link>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192990#M1657</link>
      <description>&lt;P&gt;Perhaps you could apply a 'striped' &lt;A href="https://help.autodesk.com/view/OARX/2025/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_DatabaseServices_Material" target="_blank" rel="noopener"&gt;Material&lt;/A&gt; (if you can find or create one). You apply a Material with the &lt;A href="https://help.autodesk.com/view/OARX/2025/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_DatabaseServices_Solid3d_SetSubentityMaterial_SubentityId_ObjectId" target="_blank" rel="noopener"&gt;Solid3d.SetSubentityMaterial&lt;/A&gt; method as you do for a color.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 16:59:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13192990#M1657</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-12-04T16:59:38Z</dc:date>
    </item>
    <item>
      <title>Re: i have this problem, i need solution</title>
      <link>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13193027#M1658</link>
      <description>&lt;P&gt;thank you i will try that way&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 17:11:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/i-have-this-problem-i-need-solution/m-p/13193027#M1658</guid>
      <dc:creator>dinhthit25</dc:creator>
      <dc:date>2024-12-04T17:11:28Z</dc:date>
    </item>
  </channel>
</rss>

