using EmreIstikam001.component.TehranOffice.Components._3D_DEN_2D_YE.Olculendir;
using EmreIstikam001.component.TehranOffice.Components.Other.CustomLibrary;
using Grasshopper;
using Grasshopper.Kernel;
using Rhino.Display;
using Rhino.Geometry;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Rhino;
using Rhino.FileIO;
using EmreIstikam001.component.TehranOffice.ToosForAll;
using Rhino.DocObjects;
using Rhino.Input.Custom;
using static EmreIstikam001.component.TehranOffice.ToosForAll.EIAttributesTl;
using EmreIstikam001.Properties;
using System.Windows.Forms;
using static EmreIstikam001.component.TehranOffice.Components.Other.CustomLibrary.CustomLibraryFrm;
namespace EmreIstikam001.component.TehranOffice.Components.Other.CustomLibrary
{
public class CUSTOMLIBRARY : BaseGhComponent
{
protected override string CodeName => "KUTUPHANE";
protected override GH_Exposure? ValidExposure => GH_Exposure.quinary;
private readonly CustomLibraryFrm frm = new CustomLibraryFrm();
private File3dm File;
private readonly List ShowCurves = new List();
private List points = new List();
Point3d InsertPoint = Point3d.Origin;
private string BlockName;
private ObjectAttributes BlockAttribute = new ObjectAttributes();
private List BlockAttributes = new List();
private bool ShowGeometry = false;
private bool Getpoint = false;
private bool Bake = false;
code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
var attributes = new List<ObjectAttributes>();
var geos = new List<GeometryBase>();
var doc = RhinoDoc.ActiveDoc;
foreach (var obj in File3d.Objects)
{
var attribute = new ObjectAttributes();
var layerIndex = obj.Attributes.LayerIndex;
var attribute2 = obj.Attributes;
var layerName = File3d.Layers[layerIndex].ToString();
var layerNew = doc.Layers.FindByFullPath(layerName, 0);
//attribute.LayerIndex = layerNew;
attribute2.LayerIndex = layerNew;
//attribute.ColorSource = ObjectColorSource.ColorFromObject;
//attribute.MaterialSource = ObjectMaterialSource.MaterialFromObject;
// attribute.LinetypeSource = ObjectLinetypeSource.LinetypeFromLayer;
if (obj.Geometry is Hatch)
{
var hObj = (Hatch)obj.Geometry;
var hPattern = hObj.PatternIndex;
var hpatternName = File3d.AllHatchPatterns.FindIndex(hPattern).Name;
var hPatternNew = doc.HatchPatterns.FindName(hpatternName);
hObj.PatternIndex = hPatternNew.Index;
geos.Add(hObj);
}
else
{
geos.Add(obj.Geometry);
}
attributes.Add(attribute2);
}
var blockNameNew = CreateBlockName(BlockName);
var block = CreateBlock(blockNameNew, "block description", geos, attributes);
return block;
}
public static Guid BakeBlock(string BlockName, ObjectAttributes Attribute, Transform blockTransform)
{
var guid = Guid.NewGuid();
var doc = RhinoDoc.ActiveDoc;
var Block = doc.InstanceDefinitions.Find(BlockName, false);
guid = doc.Objects.AddInstanceObject(Block.Index, blockTransform, Attribute);
return guid;
}
}
I have this code for importing from another file.
I want to refactor the importing logic to get two point from user and orient the imported file based on the points user input. something like this :
public static List Get2PointWithShow(string command, string command2, List showxCurve,
double rotate)
{
var points = new List();
Point3d pt0;
using (var getPointAction = new GetPoint())
{
getPointAction.SetCommandPrompt(command);
if (getPointAction.Get() != GetResult.Point)
{
RhinoApp.WriteLine("No select the point to draw Profile");
getPointAction.CommandResult();
}
pt0 = getPointAction.Point();
}
public static void Orient2Points(Point3d pt1, Point3d pt2, List geoCollection)
{
var moVector3d = pt1 - pt2;
var crossVector3d = Vector3d.CrossProduct(moVector3d, -Vector3d.ZAxis);
var move = Transform.PlaneToPlane(Plane.WorldXY, new Plane(pt1, -moVector3d, -crossVector3d));
foreach (var OBJ in geoCollection) OBJ.Transform(move);
}
To refactor your code to get two points from the user and orient the imported file accordingly, you need to update both your RhinoImport method and the AuthenticatedSolveInstance method.
Here's how you can modify your code:
Define Variables for the Points: In your class, declare two Point3d variables to store the user-selected points.
Update the RhinoImport Method: Modify the RhinoImport method to prompt the user for two points and store them in Point1 and Point2.
csharp
private void RhinoImport(object sender, EventArgs e)
{
// Get the first point from the user
using (var getPointAction = new GetPoint())
{
getPointAction.SetCommandPrompt("Select first point");
if (getPointAction.Get() != GetResult.Point)
{
RhinoApp.WriteLine("No point selected");
return;
}
Point1 = getPointAction.Point();
}
// Get the second point from the user
using (var getPointAction = new GetPoint())
{
getPointAction.SetCommandPrompt("Select second point");
getPointAction.SetBasePoint(Point1, true);
getPointAction.DrawLineFromPoint(Point1, true);
if (getPointAction.Get() != GetResult.Point)
{
RhinoApp.WriteLine("No second point selected");
return;
}
Point2 = getPointAction.Point();
}
Getpoint = true;
ExpireSolution(true);
}
Modify the AuthenticatedSolveInstance Method: In the AuthenticatedSolveInstance method, replace the translation transform with an orientation transform based on the two points. Apply this transformation to the imported geometries.
csharp
protected override void AuthenticatedSolveInstance(IGH_DataAccess DA)
{
frm.Show();
var filePath = frm.SelectedFilePath;
BlockName = frm.FileName;
if (!string.IsNullOrEmpty(filePath))
{
BlockName = Path.GetFileNameWithoutExtension(BlockName);
Getpoint = true;
// Read the file
double fileSuffix;
if (filePath.EndsWith(".3dm", StringComparison.InvariantCultureIgnoreCase))
{
fileSuffix = 0;
File = File3dm.Read(filePath);
}
else
{
fileSuffix = 1;
File = File3dmMood.Read(filePath);
}
var newBlockName = EIAttributesTl.EIBlocks.CreateBlockFromFile3dm(filePath, BlockName, BlockName, fileSuffix);
var doc = RhinoDoc.ActiveDoc;
var geometries = new List<GeometryBase>();
var attributes = new List<ObjectAttributes>();
var blockAttribute = new ObjectAttributes();
blockAttribute.SetUserString(BlockName, "0");
foreach (var obj in File.Objects)
{
attributes.Add(obj.Attributes.Duplicate());
geometries.Add(obj.Geometry);
}
// Apply the transformation based on the two points
Transform move = Transform.Identity;
if (Point1.IsValid && Point2.IsValid)
{
var moVector3d = Point2 - Point1;
var crossVector3d = Vector3d.CrossProduct(moVector3d, Vector3d.ZAxis);
var planeFrom = Plane.WorldXY;
var planeTo = new Plane(Point1, moVector3d, crossVector3d);
move = Transform.PlaneToPlane(planeFrom, planeTo);
foreach (var geometry in geometries)
{
geometry.Transform(move);
}
}
// Prepare geometry for showing
ShowCurves.Clear();
ShowCurves.AddRange(EIToolsTl.GeomatryBaseToCurveLIst(geometries));
// Handle baking
if (Bake)
{
ShowGeometry = false;
Getpoint = false;
Bake = false;
EIAttributesTl.EIBlocks.BakeBlock(BlockName, blockAttribute, move);
}
if (Getpoint)
{
ShowGeometry = true;
EIShowGeomatryTl.ShowGeomatry(ShowCurves, ShowGeometry, 2);
}
doc.Views.Redraw();
}
ShowGeometry = false;
}
Update the Baking Process: Ensure that the transformation is applied during the baking process by passing the move transform to the BakeBlock method.
csharp
public static Guid BakeBlock(string BlockName, ObjectAttributes Attribute, Transform blockTransform)
{
var doc = RhinoDoc.ActiveDoc;
var block = doc.InstanceDefinitions.Find(BlockName, false);
if (block != null)
{
return doc.Objects.AddInstanceObject(block.Index, blockTransform, Attribute);
}
return Guid.Empty;
}
Ensure Variables Are Properly Managed: Initialize any variables as needed and make sure the logic flow accounts for cases where points may not be set.
Note: The Orient2Points method you provided can be integrated into your code as shown in step 3, or you can call it directly if it's accessible.
Final Code Integration: By following these steps, your component will now prompt the user for two points, orient the imported geometry based on those points, and handle the display and baking accordingly.