Skip to content

Commit

Permalink
Added "Save as .obj..." dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
piac committed Jul 8, 2013
1 parent 4327ed7 commit 707b12a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 7 deletions.
3 changes: 0 additions & 3 deletions Turtle/Turtle.csproj
Expand Up @@ -49,9 +49,6 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TurtleVertex.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
5 changes: 5 additions & 0 deletions TurtleGh/GH_TurtleMesh.cs
Expand Up @@ -344,5 +344,10 @@ public override bool Write(GH_IO.Serialization.GH_IWriter writer)

return base.Write(writer);
}

public override object ScriptVariable()
{
return Value;
}
}
}
81 changes: 80 additions & 1 deletion TurtleGh/ToObjTextComponent.cs
Expand Up @@ -3,14 +3,18 @@
using Turtle;
using Turtle.Serialization;
using TurtleGh.Properties;
using System.Windows.Forms;
using System.IO;
using Grasshopper.Kernel.Attributes;

namespace TurtleGh
{
public class ToObjTextComponent : GH_Component
{
public ToObjTextComponent()
: base("To Obj Text", "ToObjT", "Gives the Obj representation of a TurtleMesh", "Mesh", "Turtle")
{ }
{
}

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
Expand All @@ -20,6 +24,23 @@ protected override void RegisterInputParams(GH_Component.GH_InputParamManager pM
pManager.AddParameter(p);
}

class SimpleChangeAttributes : GH_ComponentAttributes
{

public SimpleChangeAttributes(ToObjTextComponent cmp) : base(cmp){}

public override Grasshopper.GUI.Canvas.GH_ObjectResponse RespondToMouseDoubleClick(Grasshopper.GUI.Canvas.GH_Canvas sender, Grasshopper.GUI.GH_CanvasMouseEvent e)
{
((ToObjTextComponent)Owner).OnExport(this, EventArgs.Empty);
return base.RespondToMouseDoubleClick(sender, e);
}
}

public override void CreateAttributes()
{
Attributes = new SimpleChangeAttributes(this);
}

protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddTextParameter("Obj Text", "Obj", "The Obj text", GH_ParamAccess.item);
Expand Down Expand Up @@ -48,5 +69,63 @@ protected override System.Drawing.Bitmap Icon
return Resources.turtle_toobj;
}
}

protected override void AppendAdditionalComponentMenuItems(System.Windows.Forms.ToolStripDropDown menu)
{
base.AppendAdditionalComponentMenuItems(menu);

var item = menu.Items.Add("Save as .obj file...", null, OnExport);
item.Font = new System.Drawing.Font(item.Font.FontFamily, item.Font.Size, System.Drawing.FontStyle.Bold);
}

private void OnExport(object obj, EventArgs e)
{
if(Locked) return;

var param = Params.Output[0];

foreach (var data in param.VolatileData.AllData(true))
{
if(data.ScriptVariable() == null) continue;

var txt = data.ToString();

try
{
var sfd = new SaveFileDialog();
sfd.AddExtension = true;
sfd.Filter = "Obj File (*.obj)|*.obj|Any file (*.*)|*.*";
sfd.FilterIndex = 0;
sfd.OverwritePrompt = true;
if (OnPingDocument() != null)
{
var p = OnPingDocument().FilePath;
if (!string.IsNullOrEmpty(p) && p.Trim().Length > 0)
{
var dir = Path.GetDirectoryName(p);
if (Directory.Exists(dir))
{
sfd.InitialDirectory = dir;
}
}
}
if (sfd.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfd.FileName, txt);
}
else
break;
}
catch (Exception ex)
{
MessageBox.Show(
string.Format
("An error occurred when saving the file: {0}",
ex.Message
)
);
}
}
}
}
}
3 changes: 0 additions & 3 deletions TurtleGh/TurtleGh.csproj
Expand Up @@ -76,9 +76,6 @@
<Name>Turtle</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
Expand Down

0 comments on commit 707b12a

Please sign in to comment.