public override void Execute() { try { DsDoc = DsApp.GetActiveDocument(); DsMdl = DsDoc.GetModel(); LyM = DsDoc.GetLayerManager(); SkM = DsMdl.GetSketchManager(); SlM = DsDoc.GetSelectionManager(); DsApp.AbortRunningCommand(); CommandMessage CmdLine = DsApp.GetCommandMessage(); if (null == CmdLine) { return; } // Erreur avec le premier prompt, la commande est automatiquement annulée // Pour contourner le pb, on lance une commande à vide. { String sss = ""; CmdLine.PromptForString(true, "", "", out sss); } SelectionFilter SlFilter; SlFilter = SlM.GetSelectionFilter(); ReferenceImage Image = null; SlFilter.Clear(); SlFilter.AddEntityType(dsObjectType_e.dsReferenceImageType); SlFilter.Active = true; int NbPoint = 5000; Double Jeu = 5; Double DiamMin = 5; Double DiamMax = 45; // Double.PositiveInfinity; int SeuilNoirs = 8; int BlancPctDiam = 65; int TypeSampler = 2; int NbAffinage = 6; bool MaillageEtPoint = false; CmdLine.PromptForInteger("Nb de points maximum ", NbPoint, out NbPoint); CmdLine.PromptForDouble("Jeu entre les cercles ", Jeu, out Jeu); CmdLine.PromptForDouble("Supprimer les cercles de diam inf. à ", DiamMin, out DiamMin); CmdLine.PromptForDouble("Réduire les cercles de diam sup. à ", DiamMax, out DiamMax); CmdLine.PromptForInteger("Seuil mini pour les noirs (0 à 255) ", SeuilNoirs, out SeuilNoirs); CmdLine.PromptForInteger("Blanc, % du diam mini (0 à 100)", BlancPctDiam, out BlancPctDiam); CmdLine.PromptForInteger("Type de sampler : 1 -> Poisson / 2 -> Rejection ", TypeSampler, out TypeSampler); CmdLine.PromptForInteger("Nb d'iteration pour l'affinage ", NbAffinage, out NbAffinage); CmdLine.PromptForBool("Dessiner le maillage et les points d'origine ", "Oui", "Non", MaillageEtPoint, out MaillageEtPoint); if (CmdLine.PromptForSelection(true, "Selectionnez l'image", "Ce n'est pas une image")) { dsObjectType_e entityType; var count = SlM.GetSelectedObjectCount(dsSelectionSetType_e.dsSelectionSetType_Previous); object selectedEntity = SlM.GetSelectedObject(dsSelectionSetType_e.dsSelectionSetType_Previous, 0, out entityType); if (dsObjectType_e.dsReferenceImageType == entityType) { Image = (ReferenceImage)selectedEntity; } } //object ObjType = null; //object ObjEntites = null; //string[] TabNomsCalques = GetTabNomsCalques(DsDoc); //SkM.GetEntities(SlFilter, TabNomsCalques, out ObjType, out ObjEntites); //object[] TabEntites = ObjEntites as object[]; //Image = (ReferenceImage)TabEntites[0]; TimeSpan t; DateTime DateTimeStart; DateTimeStart = DateTime.Now; if (Image != null) { CmdLine.PrintLine(String.Format("Image : {0}", Image.GetPath())); Double ImgX, ImgY, ImgZ; Image.GetPosition(out ImgX, out ImgY, out ImgZ); CmdLine.PrintLine("Sampler"); Log.Message("Sampler"); List <PointF> listePoint; if (TypeSampler == 1) { Double fact1 = 2; Double fact2 = 0.7; CmdLine.PromptForDouble("Facteur multip. du rayon de rejection ", fact1, out fact1); CmdLine.PromptForDouble("Facteur multip. du rayon minimum à l'initialisation ", fact2, out fact2); listePoint = BitmapPoissonSampler.Run(Image, NbPoint, fact1, fact2); } else { listePoint = BitmapRejectionSampler.Run(Image, NbPoint); } dsCreateObjectResult_e res; var CalquePoint = LyM.GetLayer("Point"); if (CalquePoint == null) { LyM.CreateLayer("Point", out CalquePoint, out res); var c = CalquePoint.Color; c.SetColorByIndex(230); CalquePoint.Color = c; } if (MaillageEtPoint) { CalquePoint.Activate(); foreach (var pc in listePoint) { SkM.InsertCircleByDiameter(ImgX + pc.X, ImgY + Image.Height - pc.Y, 0, 2); } } CmdLine.PrintLine("Sampler terminé"); Log.Message("Sampler terminé"); Log.Message("Equilibrage des points"); VoronoiMap.VoronoiGraph graph; var listeSitePoincon = VoronoiEquilibreur.Start(Image, listePoint, NbAffinage, out graph); Log.Message("Equilibrage terminé"); var CalquePoincon = LyM.GetLayer("Poincon"); if (CalquePoincon == null) { LyM.CreateLayer("Poincon", out CalquePoincon, out res); var c = CalquePoincon.Color; c.SetColorByIndex(252); CalquePoincon.Color = c; } var CalqueMaillage = LyM.GetLayer("Maillage"); if (CalqueMaillage == null) { LyM.CreateLayer("Maillage", out CalqueMaillage, out res); var c = CalqueMaillage.Color; c.SetColorByIndex(126); CalqueMaillage.Color = c; } var CalqueHachures = LyM.GetLayer("Hachures"); if (CalqueHachures == null) { LyM.CreateLayer("Hachures", out CalqueHachures, out res); var c = CalqueHachures.Color; c.SetColorByIndex(100); CalqueMaillage.Color = c; } var ListeCercles = new List <Circle>(); CalquePoincon.Activate(); var facteurGris = (100 - BlancPctDiam) / (255 - SeuilNoirs); var DiamMiniDessin = Double.PositiveInfinity; var DiamMaxiDessin = 0.0; foreach (var pc in listeSitePoincon) { var diam = pc.CercleInscrit - (Jeu * 0.5); var reduce = (BlancPctDiam + (255 - pc.GrisCercleInscrit) * facteurGris) / 100; var diamReduce = Math.Min(DiamMax, diam * reduce); if (pc.GrisCercleInscrit > SeuilNoirs && diamReduce >= DiamMin) { //Log.Message("Ø : " + diam + " / f : " + reduce + " / Øred : " + diamReduce); DiamMiniDessin = Math.Min(DiamMiniDessin, diamReduce); DiamMaxiDessin = Math.Max(DiamMaxiDessin, diamReduce); var cercle = SkM.InsertCircleByDiameter(ImgX + pc.Site.X, ImgY + Image.Height - pc.Site.Y, 0, diamReduce); ListeCercles.Add(cercle); } } var format = String.Format("Nb de percages : {0} / DiamMaxi : {1:0.0} / DiamMini : {2:0.0}", ListeCercles.Count, DiamMaxiDessin, DiamMiniDessin); CmdLine.PrintLine(format); Log.Message(format); CalqueHachures.Activate(); foreach (var item in ListeCercles) { var ent = new DispatchWrapper[1] { new DispatchWrapper(item) }; SkM.InsertHatchByEntities(ent, "SOLID", 1, 0); } if (MaillageEtPoint) { CalqueMaillage.Activate(); foreach (var s in graph.Segments) { SkM.InsertLine(ImgX + s.P1.X, ImgY + Image.Height - s.P1.Y, 0, ImgX + s.P2.X, ImgY + Image.Height - s.P2.Y, 0); } } LyM.GetLayer("0").Activate(); CalqueMaillage.Shown = false; CalquePoint.Shown = false; CalquePoincon.Shown = false; } else { CmdLine.PrintLine("Pas d'image"); } t = DateTime.Now - DateTimeStart; CmdLine.PrintLine(String.Format("Executé en {0}", GetSimplestTimeSpan(t))); } catch (Exception e) { Log.Write(e); } }
public override void Execute() { try { Sm = DsApp.GetActiveDocument().GetModel().GetSketchManager(); DsApp.AbortRunningCommand(); CommandMessage CmdLine = DsApp.GetCommandMessage(); if (null == CmdLine) { return; } // Erreur avec le premier prompt, la commande est automatiquement annulée // Pour contourner le pb, on lance une commande à vide. { String sss = ""; CmdLine.PromptForString(true, "", "", out sss); } Document DsDoc = DsApp.GetActiveDocument(); Model Mdl = DsDoc.GetModel(); SketchManager SkMgr = Mdl.GetSketchManager(); SelectionManager SlMgr = DsDoc.GetSelectionManager(); SelectionFilter SlFilter; SlFilter = SlMgr.GetSelectionFilter(); object[] TabEntites = null; SlFilter.Clear(); SlFilter.AddEntityType(dsObjectType_e.dsSplineType); SlFilter.Active = true; if (CmdLine.PromptForSelection(true, "Selectionnez la spline", "Ce n'est pas une spline")) { dsObjectType_e entityType; var count = SlMgr.GetSelectedObjectCount(dsSelectionSetType_e.dsSelectionSetType_Previous); TabEntites = new object[1]; object selectedEntity = SlMgr.GetSelectedObject(dsSelectionSetType_e.dsSelectionSetType_Previous, 0, out entityType); if (dsObjectType_e.dsSplineType == entityType) { TabEntites[0] = selectedEntity; } } CmdLine.PrintLine(TabEntites.Length + " spline(s) selectionnée(s)"); TimeSpan t; DateTime DateTimeStart; DateTimeStart = DateTime.Now; if (TabEntites != null && TabEntites.Length > 0) { foreach (Spline Spline in TabEntites) { var slength = Spline.GetLength(); var Boucle = 100000; var Pas = slength / Boucle; DateTimeStart = DateTime.Now; for (double i = 0; i <= slength; i += Pas) { //CmdLine.PrintLine(i.ToString()); var ev = Spline.EvalDistance(i); //Action<iPoint, iVecteur, double> Tracer = delegate (iPoint origine, iVecteur derive, double echelle) //{ // var s = origine; // var e = origine.DeplacerC(derive.MultiplierC(echelle)); // SkMgr.InsertLine(s.X, s.Y, s.Z, e.X, e.Y, e.Z); //}; ////Tracer(ev.Point, ev.Derivee1, 100); ////Tracer(ev.Point, ev.Derivee2, 1000); } CmdLine.PrintLine(String.Format("1 Executé en {0}", GetSimplestTimeSpan(DateTime.Now - DateTimeStart))); double sP; double eP; Spline.GetEndParams(out sP, out eP); Pas = (eP - sP) / Boucle; DateTimeStart = DateTime.Now; for (double i = sP; i <= eP; i += Pas) { //CmdLine.PrintLine(i.ToString()); var ev = Spline.EvalParam(i); //Action<iPoint, iVecteur, double> Tracer = delegate (iPoint origine, iVecteur derive, double echelle) //{ // var s = origine; // var e = origine.DeplacerC(derive.MultiplierC(echelle)); // SkMgr.InsertLine(s.X, s.Y, s.Z, e.X, e.Y, e.Z); //}; ////Tracer(ev.Point, ev.Derivee1, 100); ////Tracer(ev.Point, ev.Derivee2, 1000); } CmdLine.PrintLine(String.Format("2 Executé en {0}", GetSimplestTimeSpan(DateTime.Now - DateTimeStart))); } } t = DateTime.Now - DateTimeStart; CmdLine.PrintLine(String.Format("Executé en {0}", GetSimplestTimeSpan(t))); } catch (Exception e) { Log.Write(e); } }
public override void Execute() { try { var DateTimeStart = DateTime.Now; DsApp.AbortRunningCommand(); CommandMessage CmdLine = DsApp.GetCommandMessage(); if (null == CmdLine) { return; } //============================================================================== CmdLine.PrintLine("Calque '0' actif"); Document DsDoc = DsApp.GetActiveDocument(); LayerManager LyMgr = DsDoc.GetLayerManager(); Layer L0 = LyMgr.GetLayer("0"); L0.Activate(); // Creer les calques de pliage Color c; dsCreateObjectResult_e Erreur; //============================================================================== CmdLine.PrintLine("Création du claque 'LIGNES DE PLIAGE'"); Layer LigneDePliage; LyMgr.CreateLayer("LIGNES DE PLIAGE", out LigneDePliage, out Erreur); c = LigneDePliage.Color; c.SetColorByIndex(252); LigneDePliage.Color = c; //============================================================================== CmdLine.PrintLine("Création du claque 'NOTE DE PLIAGE'"); Layer NoteDePliage = null; LyMgr.CreateLayer("NOTE DE PLIAGE", out NoteDePliage, out Erreur); c = NoteDePliage.Color; c.SetColorByIndex(126); NoteDePliage.Color = c; Model Mdl = DsDoc.GetModel(); SketchManager SkMgr = Mdl.GetSketchManager(); SelectionManager SlMgr = DsDoc.GetSelectionManager(); SelectionFilter SlFilter; object ObjType = null; object ObjEntites = null; Int32[] TabTypes = null; object[] TabEntites = null; string[] TabNomsCalques = null; //============================================================================== CmdLine.PrintLine("Couleur des entité sur 'DuCalque'"); TabNomsCalques = GetTabNomsCalques(DsDoc); SkMgr.GetEntities(null, TabNomsCalques, out ObjType, out ObjEntites); TabEntites = (object[])ObjEntites; EntityHelper dsEntityHelper = DsApp.GetEntityHelper(); foreach (object entity in TabEntites) { Color ce = dsEntityHelper.GetColor(entity); ce.SetNamedColor(dsNamedColor_e.dsNamedColor_ByLayer); dsEntityHelper.SetColor(entity, ce); } //============================================================================== CmdLine.PrintLine("Transfert des lignes et notes de pliage sur les calques correspondants"); TabNomsCalques = new string[] { "0" }; SkMgr.GetEntities(null, TabNomsCalques, out ObjType, out ObjEntites); TabTypes = (Int32[])ObjType; TabEntites = (object[])ObjEntites; for (int i = 0; i < TabEntites.GetLength(0); i++) { dsObjectType_e tpe = (dsObjectType_e)TabTypes[i]; if (tpe == dsObjectType_e.dsLineType) { Line L = (Line)TabEntites[i]; if (L.LineStyle == "SLD-Center") { L.Layer = LigneDePliage.Name; } } else if (tpe == dsObjectType_e.dsNoteType) { Note N = (Note)TabEntites[i]; N.Layer = NoteDePliage.Name; } } //============================================================================== CmdLine.PrintLine("Conversion des textes à graver en lignes"); SlFilter = SlMgr.GetSelectionFilter(); SlFilter.Clear(); SlFilter.AddEntityType(dsObjectType_e.dsNoteType); SlFilter.Active = true; TabNomsCalques = new string[] { "GRAVURE" }; SkMgr.GetEntities(SlFilter, TabNomsCalques, out ObjType, out ObjEntites); TabTypes = (Int32[])ObjType; TabEntites = ObjEntites as object[]; if (TabEntites != null && TabEntites.Length > 0) { CmdLine.PrintLine(TabEntites.Length + " texte(s) convertis"); foreach (var Texte in TabEntites) { SlMgr.ClearSelections(dsSelectionSetType_e.dsSelectionSetType_Current); dsEntityHelper.Select(Texte, true); DsApp.RunCommand("ECLATERTEXTE\n", true); } } DsApp.RunCommand("_CSICON\nP\n", true); //============================================================================== CmdLine.PrintLine("Purger le dessin"); DsApp.RunCommand("_-CLEAN\n_All\n*\n_No\n", true); TimeSpan t = DateTime.Now - DateTimeStart; CmdLine.PrintLine(String.Format("Executé en {0}", GetSimplestTimeSpan(t))); } catch (Exception e) { Log.Write(e); } }
public override void Execute() { try { Sm = DsApp.GetActiveDocument().GetModel().GetSketchManager(); DsApp.AbortRunningCommand(); CommandMessage CmdLine = DsApp.GetCommandMessage(); if (null == CmdLine) { return; } //Entree utilisateur double toleranceEcart = 0.1, toleranceAngle = 5, toleranceEcartMax = 0.1; bool methodeCalcul = true, toutesLesSplines = true, supprimerOriginal = true; // Erreur avec le premier prompt, la commande est automatiquement annulée // Pour contourner le pb, on lance une commande à vide. { String sss = ""; CmdLine.PromptForString(true, "", "", out sss); } string[] KeyWord = new string[] { "Oui", "Non", "Distance", "Max", "Angle", "Calcul", "SupprimerOriginal" }; Action <string> ModifierOptions = delegate(string s) { if (s == "Distance") { CmdLine.PromptForDouble("Deviation de distance par rapport à la spline ", toleranceEcart, out toleranceEcart); } else if (s == "Max") { CmdLine.PromptForDouble("Deviation de distance maximum absolue ", toleranceEcartMax, out toleranceEcartMax); } else if (s == "Angle") { CmdLine.PromptForDouble("Deviation d'angle par rapport à la spline ", toleranceAngle, out toleranceAngle); } else if (s == "Calcul") { CmdLine.PromptForBool("Deviation en pourcent de la longueur ", "Oui", "Non", methodeCalcul, out methodeCalcul); } else if (s == "SupprimerOriginal") { CmdLine.PromptForBool("Supprimer l'original ", "Oui", "Non", supprimerOriginal, out supprimerOriginal); } }; string Option; dsPromptResultType_e result; CmdLine.PromptExplanation = "Oui"; do { result = CmdLine.PromptForBoolOrKeyword("Convertir toutes les splines", "Erreur", KeyWord, KeyWord, (int)dsPromptInit_e.dsPromptInit_UsePromptExplanation, "Oui", "Non", true, out Option, out toutesLesSplines); switch (result) { case dsPromptResultType_e.dsPromptResultType_Keyword: ModifierOptions(Option); break; case dsPromptResultType_e.dsPromptResultType_Cancel: return; default: break; } } while (result != dsPromptResultType_e.dsPromptResultType_Value); if (result == dsPromptResultType_e.dsPromptResultType_Cancel) { return; } Document DsDoc = DsApp.GetActiveDocument(); Model Mdl = DsDoc.GetModel(); SketchManager SkMgr = Mdl.GetSketchManager(); SelectionManager SlMgr = DsDoc.GetSelectionManager(); SelectionFilter SlFilter; SlFilter = SlMgr.GetSelectionFilter(); object[] TabEntites = null; CmdLine.PrintLine($"Angle : {toleranceAngle}° Dist : {toleranceEcart}mm Max : {toleranceEcartMax}mm"); CmdLine.PrintLine($" Toutes les splines : {(toutesLesSplines ? "Oui" : "Non")} Supprimer l'original : {(supprimerOriginal?"Oui":"Non")} Calcul % : {(methodeCalcul ? "Oui" : "Non")}"); if (toutesLesSplines) { SlFilter.Clear(); SlFilter.AddEntityType(dsObjectType_e.dsSplineType); SlFilter.Active = true; object ObjType = null; object ObjEntites = null; string[] TabNomsCalques = GetTabNomsCalques(DsDoc); SkMgr.GetEntities(SlFilter, TabNomsCalques, out ObjType, out ObjEntites); TabEntites = ObjEntites as object[]; } else { SlFilter.Clear(); SlFilter.AddEntityType(dsObjectType_e.dsSplineType); SlFilter.Active = true; if (CmdLine.PromptForSelection(false, "Selectionnez la spline", "Ce n'est pas une spline")) { dsObjectType_e entityType; var count = SlMgr.GetSelectedObjectCount(dsSelectionSetType_e.dsSelectionSetType_Previous); TabEntites = new object[count]; for (int i = 0; i < count; i++) { object selectedEntity = SlMgr.GetSelectedObject(dsSelectionSetType_e.dsSelectionSetType_Previous, i, out entityType); if (dsObjectType_e.dsSplineType == entityType) { TabEntites[i] = selectedEntity; } } } } var DateTimeStart = DateTime.Now; long NbArc = 0; Action <object> Convertir = delegate(object o) { Spline Spline = o as Spline; if (Spline == null) { return; } SplineConverter SplConverter = new SplineConverter( Spline, toleranceEcart, toleranceAngle, toleranceEcartMax, methodeCalcul ? SplineConverter.eCalculTolerance.Absolu : SplineConverter.eCalculTolerance.Pourcent ); iLine?L = SplConverter.EnLigne(); if (L != null) { var l = (iLine)L; SkMgr.InsertLine(l.P1.X, l.P1.Y, l.P1.Z, l.P2.X, l.P2.Y, l.P2.Z); } else { var List = SplConverter.EnPolyligne(); foreach (var arc in List) { SkMgr.InsertArcBy3Points(arc.P1.MathPoint(), arc.P2.MathPoint(), arc.P3.MathPoint()); } NbArc += List.Count; } }; if (TabEntites != null && TabEntites.Length > 0) { CmdLine.PrintLine($"{TabEntites.Length} spline(s) selectionnées"); if (TabEntites.Length == 1) { Convertir(TabEntites[0]); } else { Parallel.ForEach(TabEntites, Convertir); } CmdLine.PrintLine($"{TabEntites.Length} spline(s) converties en {NbArc} arc(s)"); if (supprimerOriginal) { for (int i = 0; i < TabEntites.Length; i++) { Spline s = (Spline)TabEntites[i]; s.Erased = true; } } } TimeSpan t = DateTime.Now - DateTimeStart; CmdLine.PrintLine(String.Format("Executé en {0}", GetSimplestTimeSpan(t))); } catch (Exception e) { Log.Write(e); } }