Argument 2: cannot convert from 'void'

Argument 2: cannot convert from 'void'

dinhthit25
Contributor Contributor
392 Views
5 Replies
Message 1 of 6

Argument 2: cannot convert from 'void'

dinhthit25
Contributor
Contributor

i stuck at this problem Argument 2: cannot convert from 'void' to 'Autodesk.AutoCAD.Colors.Color'
does anyone know how to fix this or give me an advise how to fix this
im using C#.net framework 4.8

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 two colors.");
        return;
    }

    using (var docLock = doc.LockDocument())
    {
        PromptEntityOptions peo = new PromptEntityOptions("\nSelect a 3D solid:");
        peo.SetRejectMessage("\nOnly 3D solids are allowed.");
        peo.AddAllowedClass(typeof(Solid3d), true);
        var per = ed.GetEntity(peo);

        if (per.Status != PromptStatus.OK)
        {
            ed.WriteMessage("\nNo valid 3D solid selected.");
            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;
            }

            try
            {
                // Tạo FullSubentityPath để truy cập BREP
                var fullSubentityPath = new FullSubentityPath(new[] { per.ObjectId }, new SubentityId(SubentityType.Null, IntPtr.Zero));

                using (var brep = new Brep(fullSubentityPath))

                {
                    int faceIndex = 0;
                    foreach (Autodesk.AutoCAD.BoundaryRepresentation.Face face in brep.Faces)
                    {
                        try
                        {
                            // Lấy SubentityId của mặt
                            var subEntityId = face.SubentityPath.SubentId;

                            // Chọn màu xen kẽ dựa trên chỉ số mặt
                            var colorToApply = (faceIndex % 2 == 0) ? _color1 : _color2;

                            solid3d.SetSubentityColor(subEntityId, ApplyColorToFace(solid3d, face, colorToApply));
                          
                            faceIndex++;
                        }
                        catch (System.Exception ex)
                        {
                            ed.WriteMessage($"\nError applying color to face: {ex.Message}");
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage($"\nError processing solid: {ex.Message}");
            }

            tr.Commit();
        }
    }
}

private void ApplyColorToFace(Solid3d solid, Autodesk.AutoCAD.BoundaryRepresentation.Face face, Autodesk.AutoCAD.Colors.Color color)
{
    try
    {
        var subentityId = face.SubentityPath.SubentId;
        solid.SetSubentityColor(subentityId, color);
    }
    catch (System.Exception ex)
    {
        Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\nError applying color: {ex.Message}");
    }
}

  

0 Likes
Accepted solutions (1)
393 Views
5 Replies
Replies (5)
Message 2 of 6

ActivistInvestor
Mentor
Mentor

@dinhthit25 wrote:

i stuck at this problem Argument 2: cannot convert from 'void' to 'Autodesk.AutoCAD.Colors.Color'
does anyone know how to fix this or give me an advise how to fix this
im using C#.net framework 4.8

 

solid3d.SetSubentityColor(subEntityId, ApplyColorToFace(solid3d, face, colorToApply));

 

  


Your code is calling SetSubEntityColor() and passing it an argument that is the result of a method that returns void. 

0 Likes
Message 3 of 6

dinhthit25
Contributor
Contributor

thanks you point it out for me

0 Likes
Message 4 of 6

cuongtk2
Advocate
Advocate
Accepted solution

I don't understand why you use the ApplyColorToFace function when you could simply add one more parameter for the color in solid3d.SetSubentityColor(subEntityId, colorToApply).

0 Likes
Message 5 of 6

dinhthit25
Contributor
Contributor

may i ask do you know how to fix that line

solid3d.SetSubentityColor(subEntityId, ApplyColorToFace(solid3d, face, colorToApply));

i try changes

ApplyColorToFace

and it wont paint the color anymore

0 Likes
Message 6 of 6

dinhthit25
Contributor
Contributor

now you mention it, ApplyColorToFace not necessary

0 Likes